Just found a round problem

R

Ruby Newbee

irb(main):026:0> 10.499999999999999.round
=> 10
irb(main):027:0> 10.4999999999999999.round
=> 11


Does this happen due to the float problem in any languages including ruby?
Thanks.
 
F

Florian Gilcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


irb(main):026:0> 10.499999999999999.round
=3D> 10
irb(main):027:0> 10.4999999999999999.round
=3D> 11


Does this happen due to the float problem in any languages including =20=
ruby?
Thanks.


Thats IEEE 754 float behaviour, yes. python behaves exactly the same.

- --
Florian Gilcher

smtp: (e-mail address removed)
jabber: (e-mail address removed)
gpg: 533148E2

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.12 (Darwin)

iEYEARECAAYFAksR80wACgkQyLKU2FMxSOLkpwCdFwIj6FfsGK3+ZFFVLPQV0XwJ
Xa8AnAp6o5sxct7fsDrJUWUkIJ0QmB19
=3DbERM
-----END PGP SIGNATURE-----
 
S

Steve Wilhelm

Florian said:
Thats IEEE 754 float behaviour, yes. python behaves exactly the same.

- --
Florian Gilcher

And PHP 5 as well.

php -r 'echo sprintf ("PHP Version = %s\nround(10.499999999999999) =
%d\nround(10.4999999999999999) = %d\n", phpversion(),
round(10.499999999999999), round(10.4999999999999999));'

PHP Version = 5.2.6
round(10.499999999999999) = 10
round(10.4999999999999999) = 11
 
T

Thiago Massa

[Note: parts of this message were removed to make it a legal post.]

In C i think its 14/15 significant digits and its probably why.

If you want that much precision, find a way to use a bigger float.
 
M

Marnen Laibow-Koser

Thiago said:
In C i think its 14/15 significant digits and its probably why.

If you want that much precision, find a way to use a bigger float.

BigDecimal and Rational will do the trick.

Best,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top