Rounding number

Blitz3D Forums/Blitz3D Beginners Area/Rounding number

ILonion(Posted 2013) [#1]
Hello!

Please help me. i have absolutely other value in result.
float value (a) sometime can be integer, but I have rounding number.
Why?



;;;;;;
a_data# = 1471996
b_data = 1471996

a = CreateBank(4)
b = CreateBank(4)

PokeFloat (a,0,a_data)
PokeFloat (b,0,b_data)

Print PeekFloat (a,0)
Print PeekFloat (b,0)

WaitKey()


xlsior(Posted 2013) [#2]
Floating point numbers are always an approximation, and only a certain number of digits are accurate.

Note that that's not a blitzbasic limitation, but inherent in how floating point numbers work in any language.

If you need exact numbers, use a different datatype, like double...


ILonion(Posted 2013) [#3]
"...and only a certain number of digits are accurate."
Yes, i found exact numbers for float (in b3d): "0.....999999" by visual test :)

Thank you, xlsior!


Floyd(Posted 2013) [#4]
A good rule of thumb is to assume all floating point values are approximate. But in fact some are exact, including the one in your example.

Single precision floats have 24-bit accuracy. Integer values up to 2^24 = 16777216 are exact, including your a_data# = 1471996.

It LOOKS inexact because the display, and float-to-string conversion, rounds to six digits.
For example 12345.6789 will display as 12345.7 with the rounding.

Incidentally, you can always determine at a glance whether a displayed number is Float or Int by whether or not it has a decimal point.

Try this to see 6-digit rounded display, and 24-bit exact values, in action.

test 1471996
test 1471997
test 2^24       ; Can be represented exactly.
test 1 + 2^24   ; Can't be represented exactly, would require 25 bits.

WaitKey

Function test( x# )  ; Examine integer-valued float.
	Local i% = x
	Print
	Print x
	Print i
End Function



ILonion(Posted 2013) [#5]
I understood, thank you very much, Floyd!