Ruby floating point behavior typical of other langs??

B

bradjpeek

The second edition of _The_Ruby_Way_ has an example similar to the
following:

irb(main):001:0> puts 'not equal' unless (3.2 - 2.0) == 1.2
not equal
=>nil

The point is to illustrate why you might want to use BigDecimal (i.e.
so 3.2 - 2.0 would in fact = 1.2).

require 'bigdecimal'
x = BigDecimal("3.2")
y = BigDecimal("2.0")
z = BigDecimal("1.2")
p x - y == z ? "equal" : "not equal" # prints "equal"

I'm fairly new to Ruby and don't do much programming, but when I saw
this example I was surprised that the default behavior is that 3.2 -
2.0 != 1.2

To me, this violates the "Principal of least surprise", but I guess it
isn't a big deal because I don't remember it being discussed in
Programming Ruby book (but it certainly may have been).

Do other languages work this way?
 
W

Wilson Bilkovich

The second edition of _The_Ruby_Way_ has an example similar to the
following:

irb(main):001:0> puts 'not equal' unless (3.2 - 2.0) == 1.2
not equal
=>nil

The point is to illustrate why you might want to use BigDecimal (i.e.
so 3.2 - 2.0 would in fact = 1.2).

require 'bigdecimal'
x = BigDecimal("3.2")
y = BigDecimal("2.0")
z = BigDecimal("1.2")
p x - y == z ? "equal" : "not equal" # prints "equal"

I'm fairly new to Ruby and don't do much programming, but when I saw
this example I was surprised that the default behavior is that 3.2 -
2.0 != 1.2

To me, this violates the "Principal of least surprise", but I guess it
isn't a big deal because I don't remember it being discussed in
Programming Ruby book (but it certainly may have been).

Do other languages work this way?

Yep. This is pretty standard.
This article is tolerable, but rambles a bit:
http://en.wikipedia.org/wiki/Floating_point
 

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
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top