Can someone explain what's going on in the attached example? I'm getting 7.1 minus 7 equals .099. I need to split two-digit integers, so this result is giving me problems.
Thanks,
JN
Hi Joe
Pd uses 32 bit floating point number format which has (as any other number format) some limitations. Not every number can be exactly expressed by this format. '7.1' seems to be such a number.
In case you work with only one digit after the decimal point, I suggest to multiply your number inputs by 10 so that they are all integers. Then perform your calculations in this scale. In the end you can downscale your results again with [/ 10]. The reason is that all integers up to 2^23 (or was it 2^24?) can be represented _exactly_ by 32 bit float.
BTW: This topic has extensively been discussed several times on this list. You may find more information in the Pd-list archives.
Roman
On Thu, 2011-12-08 at 00:13 -0800, Joe Newlin wrote:
Can someone explain what's going on in the attached example? I'm getting 7.1 minus 7 equals .099. I need to split two-digit integers, so this result is giving me problems.
Thanks,
JN
-- www.joenewlin.net www.twitter.com/joe_newlin
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
On Thu, Dec 8, 2011 at 9:13 AM, Joe Newlin jtnewlin@gmail.com wrote:
Can someone explain what's going on in the attached example? I'm getting 7.1 minus 7 equals .099. I need to split two-digit integers, so this result is giving me problems.
An interesting rounding example. If you resize the numbox you would see it's not 0.099 but 0.0999999. Six significant decimal digits, that is also the maximum precision which a numbox can show.
The fraction of 7.1 is 'not terminating' when converted to binary floating point format. Compare with 1/3 which is not terminating in decimal floating point format. If you type 7.1 in a numbox or message box, 7.0999999 is in fact closer to the 32 bit binary float which represents it. But it would be annoying when you type 7.1 and it changes to 7.0999999 automatically.
I once compiled Pd with the format specifiers set to 8 significant decimal digits, and quickly found why 6 decimal digits is the default instead. Some numbers which I normally use without special consideration were automatically converted to a representation with many more digits. Type 0.02 and you get 0.019999999. It's confusing and it makes object boxes and message boxes larger than you like. On the other hand, I found that 32 bit floats can indeed represent all the integers up till 2^24. So it is a pity we can not see or type these in boxes and save them with patches. Maybe it would be better to represent the numbers 100000 and higher with 8 decimal digits.
See this IEEE 754 Converter if you want to check the rounding of a single precision float:
http://www.h-schmidt.net/FloatApplet/IEEE754.html
Katja
Le 2011-12-08 à 00:13:00, Joe Newlin a écrit :
Can someone explain what's going on in the attached example? I'm getting 7.1 minus 7 equals .099. I need to split two-digit integers, so this result is giving me problems.
in fractional powers of two,
1/10 = 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + 1/65536 + ...
in fractional binary digits, it's written like 0.000110011001100110011... it's periodic. However, a float is never periodic, so, the digits are chopped.
| Mathieu BOUCHARD ----- téléphone : +1.514.383.3801 ----- Montréal, QC
Thanks Roman, Mathieu et al for the illuminating replies.
On Dec 8, 2011, at 7:57 AM, Mathieu Bouchard matju@artengine.ca wrote:
Le 2011-12-08 à 00:13:00, Joe Newlin a écrit :
Can someone explain what's going on in the attached example? I'm getting 7.1 minus 7 equals .099. I need to split two-digit integers, so this result is giving me problems.
in fractional powers of two,
1/10 = 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + 1/65536 + ...
in fractional binary digits, it's written like 0.000110011001100110011... it's periodic. However, a float is never periodic, so, the digits are chopped.
| Mathieu BOUCHARD ----- téléphone : +1.514.383.3801 ----- Montréal, QC