# Inaccurate float multiplication: 0.0001 * 3.0 = 0.00030000000000000003

I apologize if this is documented somewhere. I looked but couldn't find it.

$number1 = 0.0001 * 1$number2 = 0.0001 * 2
$number3 = 0.0001 * 3 notify{"$number1 $number2$number3":}

Notice['0.0001 0.0002 0.00030000000000000003']


I thought this might be an integer to float conversion issue but I get the same result with:

$number1 = 0.0001 * 1.0$number2 = 0.0001 * 2.0
$number3 = 0.0001 * 3.0 notify{"$number1 $number2$number3":}
Notice[0.0001 0.0002 0.00030000000000000003]


I'm not very good at math, but this doesn't look right =)

Does anyone have a solution for getting consistent and accurate results?

edit retag close merge delete

Sort by ยป oldest newest most voted

That is not a problem of puppet but of how floats work (have a look around the net for comprehensive explanations).

Basic float 101:

numbers are stored as binary data: 11_dec ~ 1011_bin because 1*8+0*4+1*2+1*1 = 11 = 1*10+1*1

for fractions this becomes 0.5_dec ~ 0.1_bin because 1/2 = 5/10 = 0.5
now try to build 0.3 as sum of 1/(2^n). It will always be an approximation. You can just choose how good it will be.

more

Thank you for explaining this

( 2016-08-31 09:33:54 -0600 )edit