[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Numbers

djreid

6/9/2006 9:23:00 AM

Hi I'm new to ruby.
Started with learning-ruby.
In first_steps it looks at using ruby as a calculator
>> 1 + 2
=> 3
>> 3 / 2
=> 1
>> 3.0 / 2

=> 1.5

all this I understand BUT

>> 4.1 % 2

=> 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
>> 3.945 * 2.62

=> 10.3359 4 decimals should only be three

Dave

6 Answers

Todd

6/9/2006 3:50:00 PM

0

Dave wrote:
> Hi I'm new to ruby.
> Started with learning-ruby.
> In first_steps it looks at using ruby as a calculator
> >> 1 + 2
> => 3
> >> 3 / 2
> => 1
> >> 3.0 / 2
>
> => 1.5
>
> all this I understand BUT
>
> >> 4.1 % 2
>
> => 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
> >> 3.945 * 2.62
>
> => 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

Roger

6/9/2006 6:03:00 PM

0

> >> 4.1 % 2
>

Modulo operator: for integers

Roger

Roger

6/9/2006 6:13:00 PM

0

Roger wrote:
> > >> 4.1 % 2
> >
>
> Modulo operator: for integers
>
> Roger

This from the Ruby docs:
http://www.ruby-doc.org/core/classes/Float.ht...
so, i guess above response is wrong.

flt % other => float
flt.modulo(other) => float

Return the modulo after division of flt by other.

6543.21.modulo(137) #=> 104.21
6543.21.modulo(137.24) #=> 92.9299999999996

djreid

6/9/2006 9:50:00 PM

0


Todd wrote:
>
> 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

Just Another Victim of the Ambient Morality

6/10/2006 4:55:00 AM

0

I quoted another post of yours for more context.

"Dave" <djreid@ihug.co.nz> wrote in message
news:1149889807.437723.251690@u72g2000cwu.googlegroups.com...
>
>> 4.1 % 2
> >
>> => 0.0999999999999996
>
> 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...


Todd

6/20/2006 12:16:00 AM

0

Just Another Victim of the Ambient Morality wrote:
> I quoted another post of yours for more context.
>
> "Dave" <djreid@ihug.co.nz> wrote in message
> news:1149889807.437723.251690@u72g2000cwu.googlegroups.com...
> >
> >> 4.1 % 2
> > >
> >> => 0.0999999999999996
> >
> > Still got to sort 4.1 % 2 still say it should be .1
>
> 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