Re: Using ReplaceAll (/.) on numerical digits
- To: mathgroup at smc.vnet.net
- Subject: [mg113278] Re: Using ReplaceAll (/.) on numerical digits
- From: telefunkenvf14 <rgorka at gmail.com>
- Date: Thu, 21 Oct 2010 07:04:41 -0400 (EDT)
- References: <i9m854$jr5$1@smc.vnet.net>
On Oct 20, 3:10 am, Richard Klopp <rkl... at exponent.com> wrote: > Background: > I made some lab measurements and then calibrated the test instrument after > the fact. This changed the measurements slightly. I wanted to replace the > measurements with the calibrated values. The measurements were all very > close to 0.5600, only differing in the thousandths and ten-thousandths > places, so I coded my original data as a list of two-digit integers > {20,30,40}, then divided the whole list by 10000 and added 0.5600 to it. > > I found some seemingly weird behavior with ReplaceAll as shown below. Note > how 0.563 in data remains untouched while 0.562 and 0.564 get replaced as > intended, but when I simply type in 0.563 and do a replace, it gets replaced > successfully. Can someone explain why this happens? > > In any event, I've got a workaround based on StringReplace, which is likely > safer whether the weird behavior is a bug or a misunderstanding on my part. > > data = 0.5600 + 1/10000 {20, 30, 40} {0.562, 0.563, 0.564} (* 0.563 stays > untouched *) data /. {0.562 -> 0.5622, 0.563 -> 0.5637, 0.564 -> 0.5641} > {0.5622, 0.563, 0.5641} (* 0.563 stays untouched *) (0.5600 + 1/10000 {20, > 30, 40}) /. {0.562 -> 0.5622, 0.563 -> 0.5637, 0.564 -> 0.5641} {0.5622, > 0.563, 0.5641} (* 0.563 stays untouched *) {0.5600 + 20/10000, 0.5600 + > 30/10000, 0.5600 + 40/10000} /. {0.562 -> 0.5622, 0.563 -> 0.5637, 0.564 > -> 0.5641} {0.5622, 0.563, 0.5641} (* 0.563 gets replaced when typed in > directly *) {0.562, 0.563, 0.564, 0.5630} /. {0.562 -> 0.5622, 0.563 -> > 0.5637, 0.564 -> 0.5641} {0.5622, 0.5637, 0.5641, 0.5637} > > (*workaround*) replaceDigits[numList_List, replacementDigitsList_List] := ToExpression[ StringReplace[ ToString[PaddedForm[#, {5, 5}] & /@ > numList], Map[ToString[PaddedForm[#, {5, 5}]] &, replacementDigitsList, > {2}] ] ] replaceDigits[data, {0.562 -> 0.5622, 0.563 -> 0.5637, 0.564 > -> 0.5641}] {0.5622, 0.5637, 0.5641} Why does this happen? Floating point arithmetic isn't exact. Suggestions: Try using Rationalize[]. Or set up the expression as a function, with symbolic placeholders. That way you can simply input the calibrated values into the function. -RG