Velocity Reviews > Ruby > Trig value errors

# Trig value errors

Phillip Gawlowski
Guest
Posts: n/a

 12-23-2009
On 23.12.2009 08:15, jzakiya wrote:

> This didn't nullify THE POINT I illustrated, which was
> using an increasingly smaller angle approaching zero as
> an argument, it eventually produced a value of '1' for
> cos, but not '0' for the same sin angle, as it should.

How many real world (as in, reality, out in the wild, where money and
lives matter) cases do you know where a) Ruby is used for these
calculations and b) these calculations matter?

> I feel I've made my points as clear as I think is needed.
> If it doesn't bother you that the present implementation produces
> incorrect results for clearly unambiguous
> situations then we just have different standards for the
> need to adhere to mathematical rigor and produce exact
> results. BUT THAT IS A CHOICE THAT HUMANS MAKE, AND IS
> NOT MANDATED BY NATURE.

A core tenent of mathematical rigor is consistency. Ruby *is*
consistent. The results are, within the limitations already elaborated
on earlier, correct, too.

If you need higher accuracy, you need a different tool.

Now to something no mathematician worth his or her salt likes to hear:
Math is *not* observed in nature. Math is a support science for physics,
to help *describe* actual, observeable, phenomena, and express theories
and formulae in an unambiguous manner, as well as in an abstract manner
to allow the manipulation of said formulae.

--
Phillip Gawlowski

Paul Smith
Guest
Posts: n/a

 12-23-2009
On Wed, Dec 23, 2009 at 2:03 PM, Phillip Gawlowski <(E-Mail Removed)> wrote:
> On 23.12.2009 08:15, jzakiya wrote:
>
>> This didn't nullify THE POINT I illustrated, which was
>> using an increasingly smaller angle approaching zero as
>> an argument, it eventually produced a value of '1' for
>> cos, but not '0' for the same sin angle, as it should.

No, it didn't.

irb(main):020:0> x = cos(onedegree/1e06)
=> 1.0
irb(main):021:0> x - 1.0
=> -1.11022302462516e-016

In the first line, I assign x the value of cos(small). The irb output
of that number is 1.0. In the second line however, I show that x
cannot be equal to 1.0, as there is a non-zero difference between
them. (This is why you never check for equality of floats like this)

So, cos(small) only looks like 1.0, it isn't actually equal to 1.0.
Therefore it's not really much surprise than sin(small) isn't 0.

--
Paul Smith

http://www.velocityreviews.com/forums/(E-Mail Removed)

Colin Bartlett
Guest
Posts: n/a

 12-23-2009
jzakiya wrote
> We know cos(PI/2)=0 not 6.123023176911189e-17,
> so round it, truncate, whatever, to 0.

But Ruby PI is not (exactly) the mathematical pi,
so it's going to be difficult to decide in cos(x),
with x very close to pi/2, whether x was "really" pi/2,
or something a bit different from pi/2 ?

Phrogz wrote:
> I thought asking Alpha would be an easy place to start.
> ...
> http://www.wolframalpha.com

I didn't know about WolframAlpha! Thanks!
sin(pi/2) #=> 1
tan(pi/2) #=> infinity; so far, so good
sin(pi*0.5) #=> 1
sin(pi*0.5) - 1 #=> 0 "number name zero", so it is zero!
tan(pi*0.5) #=> 1.63312 x 10^16
(Actually in some ways I find that last result
a little surprising, because floating point
holds 0.5 exactly. But also not surprising!)

Since Fleck Jean-Julien mentioned Octave:
> Well, what you are looking for is ... or Octave,
> some kind of symbolic mathematics software.
> ... But even there you can still quite easily find
> "mathematical bugs" that is results that are not in the
> simplest form expected even by my least gifted students.

A naive user (me) of Octave gets:
sin(pi/2) #=> 1
cos(pi/2) #=> 6.123....189e-017
(By naive, I mean that a more experienced user
might be able to do better than that.)

Colin Bartlett
Guest
Posts: n/a

 12-23-2009
Robert Klemme wrote:
> ... Not sure what the term "nature" means here.
> AFAIK mathematics are a human invention ...

Are you *sure* about that? I think there are differing views
even amongst mathematicians: some agree with you,
but I think the majority vote (for now) is for "Realism",
in one of its several flavours.
http://en.wikipedia.org/wiki/Philosophy_of_mathematics

Phillip Gawlowski wrote:
> Now to something no mathematician worth his or her salt
> likes to hear: Math is *not* observed in nature.
> Math is a support science for physics,
> to help *describe* actual, observeable, phenomena,
> and express theories and formulae in an unambiguous manner,
> as well as in an abstract manner to allow
> the manipulation of said formulae.

That's a bit limiting: it's also very useful in engineering,
chemistry, etc, etc! And for a "support science" it does
rather well, frequently discovering/inventing (depending
on your philosophy of maths) things several decades
before physicists realised they needed them!
But to be fair, there is a two-way traffic.

(A warning for readers: my degree is in maths,
but I am not a mathematician!)

Xavier Noria
Guest
Posts: n/a

 12-23-2009

Ordinary programming language use floats, with all their limitations
and virtues. If you want something closer to exact math please look
elsewhere like Mathematica.

Phillip Gawlowski
Guest
Posts: n/a

 12-23-2009
On 23.12.2009 17:32, Colin Bartlett wrote:

> Phillip Gawlowski wrote:
>> Now to something no mathematician worth his or her salt
>> likes to hear: Math is *not* observed in nature.
>> Math is a support science for physics,
>> to help *describe* actual, observeable, phenomena,
>> and express theories and formulae in an unambiguous manner,
>> as well as in an abstract manner to allow
>> the manipulation of said formulae.

>
> That's a bit limiting: it's also very useful in engineering,
> chemistry, etc, etc! And for a "support science" it does
> rather well, frequently discovering/inventing (depending
> on your philosophy of maths) things several decades
> before physicists realised they needed them!
> But to be fair, there is a two-way traffic.

Well, if me and my professors had a choice, we wouldn't bother with
maths (I'm in college to get an engineering degree), so I'm quiet
oviously jsut a little biased.

And while maths, unlike other support sciences *does* have decent, and
interesting research all of its own, if it weren't for sciences that
need maths, nobody would need it, and it would find itself in the same
thankless corner as philosophy: Undervalued as well as underappreciated.

For example, I like the beauty of maths in music, and the visualization
of Mandelbrot fractals, and feel that maths can stand on its own very,
very well.

Mind, I'm not putting down maths as a field of science, at all! It has
established itself firmly in the science community, otherwise there
wouldn't be dedicated math degrees.

And it is indeed true that maths makes life easier, for quite near everyone.

--
Phillip Gawlowski

jzakiya
Guest
Posts: n/a

 12-23-2009
Dear Matz,

I use a Linux calculator named Qaculate.
I do not know the people who designed and
coded it, but it makes me happy. Why?

When I put in sin(180) it outputs '0'
not 1.22460635382238e-16, and when I change
to radians for angles and do sin(PI) it
also gives '0' and not 1.22460635382238e-16.

This makes me happy.

In fact, this little Linux calculator produces
all the correct (exact) answers for angles on
an axis for both cos and sin, whether you input
the angles in degrees or radians. Astonishing!!

I don't now anything about how the people who
designed and implemented Qalculate decided to
use whatever language they chose to write it in,
or what libraries they decided to use. I don't
know (or care) about any of those under-the-hood
things.

What I do KNOW is that the people who designed and
implemented this calculator CARED that it produced
the mathematically exact results for those angles.

This makes me happy.

I use another add-on calculator for Firefox, called,
tada! Calculator. Again, I don't know the people who
designed and/or coded it, and I don't know what
language or libraries they used to do it in either.
But when I use this little calculator in Firefox I do
know I get sin(PI) = 0, not 1.22460635382238e-16.

So, for this calculator too, somebody(s) CARED enough
to make sure the answers came out correctly (exact)
for angles on an axis too. So when I use this calculator,

This makes me happy.

I think Ruby is a Great language, and a fun language.
And I agree with your philosophy that languages that
people use should serve them, not the other way around.

So I humbly tell you I am Surprised that Ruby produces
the math errors I've illustrated in this thread, and

This makes me unhappy.

So I am asking you to see that these errors be fixed.

If Qaculator and Calculator can produce the correct
results, then so can Ruby. All that is necessary is that
you CARE enough that it does it.

It would make me very happy if I could share my code, and
not have to include patches and redefinitions in it just
to assure other people get the same (exact) results I do.

I agree with your Principle of Least Surprises (POLS).

The language should work to please the user.
The language should be intuitive to the user.
The language should not cause unnecessary surprises.

So please, fix these errors.

This will make me happy.

Thanks

pharrington
Guest
Posts: n/a

 12-23-2009
On Dec 23, 12:14*pm, Phillip Gawlowski <(E-Mail Removed)> wrote:
> On 23.12.2009 17:32, Colin Bartlett wrote:
>
> > Phillip Gawlowski wrote:
> >> Now to something no mathematician worth his or her salt
> >> likes to hear: Math is *not* observed in nature.
> >> Math is a support science for physics,
> >> to help *describe* actual, observeable, phenomena,
> >> and express theories and formulae in an unambiguous manner,
> >> as well as in an abstract manner to allow
> >> the manipulation of said formulae.

>
> > That's a bit limiting: it's also very useful in engineering,
> > chemistry, etc, etc! And for a "support science" it does
> > rather well, frequently discovering/inventing (depending
> > on your philosophy of maths) things several decades
> > before physicists realised they needed them!
> > But to be fair, there is a two-way traffic.

>
> Well, if me and my professors had a choice, we wouldn't bother with
> maths (I'm in college to get an engineering degree), so I'm quiet
> oviously jsut a little biased.
>
> And while maths, unlike other support sciences *does* have decent, and
> interesting research all of its own, if it weren't for sciences that
> need maths, nobody would need it.

Now that we're thoroughly off topic

This is like saying if people didn't need to eat, there'd be no need
for food. Just because a statement is true does not mean it is
relevant.

There isn't a single field of research that yields practical
application who's core isn't founded in some sort of math. I'm not a
mathematician (or even a math major) but my understanding has always
been that maths have been *discovered*, not necessarily *invented*.

pharrington
Guest
Posts: n/a

 12-23-2009
On Dec 23, 12:35*pm, jzakiya <(E-Mail Removed)> wrote:
> Dear Matz,
>
> I use a Linux calculator named Qaculate.
> I do not know the people who designed and
> coded it, but it makes me happy. Why?
>
> When I put in sin(180) it outputs '0'
> not 1.22460635382238e-16, and when I change
> to radians for angles and do sin(PI) it
> also gives '0' and not 1.22460635382238e-16.
>
> This makes me happy.
>
> In fact, this little Linux calculator produces
> all the correct (exact) answers for angles on
> an axis for both cos and sin, whether you input
> the angles in degrees or radians. Astonishing!!
>
> I don't now anything about how the people who
> designed and implemented Qalculate decided to
> use whatever language they chose to write it in,
> or what libraries they decided to use. I don't
> know (or care) about any of those under-the-hood
> things.
>
> What I do KNOW is that the people who designed and
> implemented this calculator CARED that it produced
> the mathematically exact results for those angles.
>
> This makes me happy.

Perhaps you should look under the hood to see what its doing.
>
> I use another add-on calculator for Firefox, called,
> tada! Calculator. Again, I don't know the people who
> designed and/or coded it, and I don't know what
> language or libraries they used to do it in either.
> But when I use this little calculator in Firefox I do
> know I get sin(PI) = 0, not 1.22460635382238e-16.
>
> So, for this calculator too, somebody(s) CARED enough
> to make sure the answers came out correctly (exact)
> for angles on an axis too. So when I use this calculator,
>
> This makes me happy.

***PURE*** speculation, but this sounds like its just rounding off the
results for a pretty display for the end user. (Again, look at the
source)
>
> I think Ruby is a Great language, and a fun language.
> And I agree with your philosophy that languages that
> people use should serve them, not the other way around.
>
> So I humbly tell you I am Surprised that Ruby produces
> the math errors I've illustrated in this thread, and
>
> This makes me unhappy.
>
> So I am asking you to see that these errors be fixed.
>
> If Qaculator and Calculator can produce the correct
> results, then so can Ruby. All that is necessary is that
> you CARE enough that it does it.
>
> It would make me very happy if I could share my code, and
> not have to include patches and redefinitions in it just
> to assure other people get the same (exact) results I do.
>
> I agree with your Principle of Least Surprises (POLS).
>
> The language should work to please the user.
> The language should be intuitive to the user.
> The language should not cause unnecessary surprises.
>
> So please, fix these errors.
>
> This will make me happy.
>
> Thanks

This has already been explained. The ratio of a circle's circumference
to its diameter cannot be fully actualized in finite bytes (and
certainly not in floats). Calculations using Math:I, therefor,
unless given a special representation, and the language/library has
been built from the ground up to handle calculations with that special
representation (and similarly with all other sorts of irrational
numbers) will never yield a 100% result. If you need more precision
than floating point arithmatic provides, use a specialized language.
If you want your values that, for you, are sufficiently close to zero
to be zero, then its easy enough to round them. However, that decision
****CANNOT**** be made for everybody using the language, as different
people have different expectations of precision, and as has been
mentioned before libraries will break.

pharrington
Guest
Posts: n/a

 12-23-2009
On Dec 23, 12:35*pm, jzakiya <(E-Mail Removed)> wrote:
> Dear Matz,
>
> I use a Linux calculator named Qaculate.
> I do not know the people who designed and
> coded it, but it makes me happy. Why?
>
> When I put in sin(180) it outputs '0'
> not 1.22460635382238e-16, and when I change
> to radians for angles and do sin(PI) it
> also gives '0' and not 1.22460635382238e-16.
>
> This makes me happy.
>
> In fact, this little Linux calculator produces
> all the correct (exact) answers for angles on
> an axis for both cos and sin, whether you input
> the angles in degrees or radians. Astonishing!!
>
> I don't now anything about how the people who
> designed and implemented Qalculate decided to
> use whatever language they chose to write it in,
> or what libraries they decided to use. I don't
> know (or care) about any of those under-the-hood
> things.
>
> What I do KNOW is that the people who designed and
> implemented this calculator CARED that it produced
> the mathematically exact results for those angles.
>
> This makes me happy.
>
> I use another add-on calculator for Firefox, called,
> tada! Calculator. Again, I don't know the people who
> designed and/or coded it, and I don't know what
> language or libraries they used to do it in either.
> But when I use this little calculator in Firefox I do
> know I get sin(PI) = 0, not 1.22460635382238e-16.
>
> So, for this calculator too, somebody(s) CARED enough
> to make sure the answers came out correctly (exact)
> for angles on an axis too. So when I use this calculator,
>
> This makes me happy.
>
> I think Ruby is a Great language, and a fun language.
> And I agree with your philosophy that languages that
> people use should serve them, not the other way around.
>
> So I humbly tell you I am Surprised that Ruby produces
> the math errors I've illustrated in this thread, and
>
> This makes me unhappy.
>
> So I am asking you to see that these errors be fixed.
>
> If Qaculator and Calculator can produce the correct
> results, then so can Ruby. All that is necessary is that
> you CARE enough that it does it.
>
> It would make me very happy if I could share my code, and
> not have to include patches and redefinitions in it just
> to assure other people get the same (exact) results I do.
>
> I agree with your Principle of Least Surprises (POLS).
>
> The language should work to please the user.
> The language should be intuitive to the user.
> The language should not cause unnecessary surprises.
>
> So please, fix these errors.
>
> This will make me happy.
>
> Thanks

Also, as an example:

static VALUE
math_cos(VALUE obj, VALUE x)
{
Need_Float(x);
return DBL2NUM(cos(RFLOAT_VALUE(x)));
}

If you have a beef with anyone, its with the implementers of the C
standard library, but...