Ask Your Question
0

Inaccurate float multiplication: 0.0001 * 3.0 = 0.00030000000000000003

asked 2016-08-25 14:21:21 -0500

AmberW gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-08-29 04:16:50 -0500

pascal gravatar image

updated 2016-08-29 04:24:01 -0500

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.
edit flag offensive delete link more

Comments

Thank you for explaining this

AmberW gravatar imageAmberW ( 2016-08-31 09:33:54 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-08-25 14:21:21 -0500

Seen: 38 times

Last updated: Aug 29 '16