Ruby Numbers

D

Dave

Hi I'm new to ruby.
Started with learning-ruby.
In first_steps it looks at using ruby as a calculator
=> 1.5

all this I understand BUT

=> 0.0999999999999996

Should be .1
and whats more there has only been one decimal place used so the answer
should have only 1 decimal place.

another
=> 10.3359 4 decimals should only be three

Dave
 
T

Todd

Dave said:
Hi I'm new to ruby.
Started with learning-ruby.
In first_steps it looks at using ruby as a calculator

=> 1.5

all this I understand BUT


=> 0.0999999999999996

Should be .1
and whats more there has only been one decimal place used so the answer
should have only 1 decimal place.

Not that this is a reliable source, but from wikipedia "Modular
arithmetic (sometimes called modulo arithmetic) is a system of
arithmetic for integers." I'm not sure, but I think the problem you
are running into here is how Ruby handles floating point values. You
could work around this by turning all of your floating point numbers
into large integers and converting back after the mod operation. In
fact, it wouldn't surprise me if a library existed for such a thing.
another

=> 10.3359 4 decimals should only be three

in irb, 1.0/3 = 0.333333333333333

Would you prefer the result to be 0.3? I don't think I'd want a
calculator, handheld or otherwise, that 3.945 * 2.62 would result in
10.335 (truncation/rounding down) or 10.336 (rounding up). You can do
those things yourself.

Todd
 
D

Dave

Todd said:
in irb, 1.0/3 = 0.333333333333333

Would you prefer the result to be 0.3? I don't think I'd want a
calculator, handheld or otherwise, that 3.945 * 2.62 would result in
10.335 (truncation/rounding down) or 10.336 (rounding up). You can do
those things yourself.

Todd


No I didn't say (type) that I wanted it to be to three or decimals, I
just said I thought it would be. But I can see the error of my
thinking, as soon as you enter into the realm of floats ruby will float
as many decimals as it needs.

Still got to sort 4.1 % 2 still say it should be .1


Dave
 
J

Just Another Victim of the Ambient Morality

I quoted another post of yours for more context.

Dave said:
Still got to sort 4.1 % 2 still say it should be .1

Actually, it is 0.1 or, rather, close enough. I'm not intimately
familiar with the implementation of Ruby but my best guess is that Ruby
converted the value 4.1 into the IEEE double floating point representation
and did the relevant operations using that value. The problem is that the
IEEE format cannot accurately represent the value 4.1, representing it as
4.0999999999999996 instead, resulting in the final mod result being as you
see it. This isn't really even a problem specific to the IEEE standard,
considering how Real numbered values simply cannot (hopefully, for obvious
reasons) be accurately represented in finite digital circuitry.
This is exactly why you are always told to never try to directly
compare, with the equals operator, floating point values, in any language.
You will often fail to find equality when you expected it. Instead, you
should specify a delta value to test whether floating point values are
"close enough."
You can see this for yourself if you have access to a C compiler and
debugger. Simply assign the value 4.1 to a variable of type double and then
examine the value of that variable. I think you will be surprised...
 
T

Todd

Just said:
I quoted another post of yours for more context.



Actually, it is 0.1 or, rather, close enough.

Although slightly unrelated, I like the similarity with this old
conundrum:

1.0/9.0 = 0.11111111...
8.0/9.0 = 0.88888888...
1.0/9.0 + 8.0/9.0 maybe should equal 0.99999999... in floating point
arithmetic on a computer?

irb result: 1.0/9.0 + 8.0/9.0 = 1.0

I haven't really looked at the internal workings of Ruby, or the
hosting kernel, for that matter, but it looks like there is some
perception of infinity built in here--even with floating point
numbers--or is it simply just rounding? I guess I'll have to do some
research here, because I'm very curious, and not just on a software
level. Anybody have some links/pointers on digitally dealing with
these kind of things?

Todd
 

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,206
Messages
2,571,069
Members
47,677
Latest member
MoisesKoeh

Latest Threads

Top