Simple question...

CT_Biker

Expert Member
Joined
Sep 10, 2016
Messages
2,925
Reaction score
1,824
Location
Cape Town
Floating point== Logarithm

or am I seeing things

ps. im really bad at maths
 
now I am really confused.

why does floating point arithmatic use all a lot of memory and why is it slightly inaccurate?

sorry if I am being weird, alcohol.
 
now I am really confused.

why does floating point arithmatic use all a lot of memory and why is it slightly inaccurate?

sorry if I am being weird, alcohol.

Not sure what you mean by logarithm here. The exponent bits of the FP number is the toward-zero rounded log2 of the number it is trying to represent. The mantisa bits interpolate the value between the exp2 of this and the exp2 of the toward-infinity log2 rounding of the number.

It doesn’t use a lot of memory. Single precision FP is 4 bytes and double precision is 8 bytes.

It’s “inaccurate” in the sense that if the result of an operation (E.g., add or multiply) is not representable by the floating point type, the result will be rounded to a nearby representable value.
 
Last edited:
Inaccuracy isn't just a matter of how many bytes are reserved for storing single / double precision numbers.

In maths the inability to capture exact precision relates to irrational numbers, for example:
  • Pi π (3.14159...)
  • Euler's number e,
  • Golden Ratio φ,
  • Logs: We can prove the natural logarithm of all integers ≥ 2 are irrational, and that the decimal logarithm of any integer is irrational except for the powers of 10.
...and because we can't quantify all the decimals for irrational numbers, we are left with no choice but to round to a decimal precision that fits within the available bytes.
 
Last edited:
As Cguy pointed out that floats ate calculated using log2(base 2) which is most likely why 0.2 + 0.1 is not quite 0.3 as it we would normally express it.

the reason why I asked if it using floating point/double precision numbers are similar to logs is because of the really odd answers I have been getting when using *insert language* for doing float point arithmatic.

I never knew that 1) floating point used base 2 - I need to read up on this, but I suspect it pairs number in a binary like manner, and 2) floats/double precision number are essentially scientific notation, from what I can see hence why I asked if it is similar or equal to expressing a number with a lot of decimals using a Logarithm and 3) I guess this is why standard integers should be used, especially when working with currency or time.
 
Last edited:
As Cguy pointed out that floats ate calculated using log2(base 2) which is most likely why 0.2 + 0.1 is not quite 0.3 as it we would normally express it.

the reason why I asked if it using floating point/double precision numbers are similar to logs is because of the really odd answers I have been getting when using *insert language* for doing float point arithmatic.

I never knew that 1) floating point used base 2 - I need to read up on this, but I suspect it pairs number in a binary like manner, and 2) floats/double precision number are essentially scientific notation, from what I can see hence why I asked if it is similar or equal to expressing a number with a lot of decimals using a Logarithm and 3) I guess this is why standard integers should be used, especially when working with currency or time.

Correct, is pretty close to scientific notation, it’s just base 2 instead of base 10. For currency I typically use a 64-bit integer representing cents and for time a 64-bit integer for representing nanoseconds. Floating point is used for data that has a high dynamic range. I.e., the ratio of the largest to smallest number in my data is very large, and has the property that more precision is needed for smaller values than larger values.
 
**** you people on a Friday night...now my brain hurts reading this ****.
 
Correct, is pretty close to scientific notation, it’s just base 2 instead of base 10. For currency I typically use a 64-bit integer representing cents and for time a 64-bit integer for representing nanoseconds. Floating point is used for data that has a high dynamic range. I.e., the ratio of the largest to smallest number in my data is very large, and has the property that more precision is needed for smaller values than larger values.

That makes perfect sense, at least I know to avoid this mistake and correct if i see others do it. Thanks for the clarity and advice.
 
**** you people on a Friday night...now my brain hurts reading this ****.

A little Gin helps. I have been feeling disgruntled with my progress with Skilling up, this was the first thing I noticed after I had idea that coding at 2 am would be fun.
 
Correct, is pretty close to scientific notation, it’s just base 2 instead of base 10. For currency I typically use a 64-bit integer representing cents and for time a 64-bit integer for representing nanoseconds. Floating point is used for data that has a high dynamic range. I.e., the ratio of the largest to smallest number in my data is very large, and has the property that more precision is needed for smaller values than larger values.

If I may just, this would be in situations where round to the nearest second or third decimal will negate the computational error, this explains why a lot people miss this little oddity.
 
If I may just, this would be in situations where round to the nearest second or third decimal will negate the computational error, this explains why a lot people miss this little oddity.


1.29999999999998
 
If I may just, this would be in situations where round to the nearest second or third decimal will negate the computational error, this explains why a lot people miss this little oddity.

Yup, and it’s also the source of bugs. Take the 1.299999999 above - rounded, this would print as 1.3, but it could fail a check for >= 1.3 somewhere else.
 
This also becomes tricky when you have Invoice Lines with UnitPrice * Qty with a % based discount on the line. Then multiple lines then add VAT.
 
This also becomes tricky when you have Invoice Lines with UnitPrice * Qty with a % based discount on the line. Then multiple lines then add VAT.

i think its pretty common in cases where there are multiples to round one down to match a solid figure.
 
i think its pretty common in cases where there are multiples to round one down to match a solid figure.

Correct, there is an accepted 'best' way. But I found not many devs or end users get that.
 
Top
Sign up to the MyBroadband newsletter
X