For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > March 2008 > floating point issue









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author floating point issue
Chris Scullard

2008-03-20, 4:41 am

Hi everyone,

I wonder if I can get some opinions on the best way to deal with this
precision issue I am having. I define the vectors:

K = {111.5, 10.5, 1.5}
g={-0.7071068, 0., -0.7071068}

And I need this:

K.Cross[K, g]

to be 0 in accordance with a vector identity. The answer comes out to
around 1.3 x 10^(-13), which is certainly close to 0 but not close
enough for what I'm doing. I've tried various things like writing out
the cross product explicitly without using the functions but the result
is the same. And using N in various places doesn't seem to help either.
What's the standard solution for this kind of thing?

Thanks,
Chris

Antti Penttilä

2008-03-20, 8:25 am

Hi,

It's 0 for me (WinXP 64-bit, Mathematica 6.0.2).

Antti

Chris Scullard wrote:
> Hi everyone,
>
> I wonder if I can get some opinions on the best way to deal with this
> precision issue I am having. I define the vectors:
>
> K = {111.5, 10.5, 1.5}
> g={-0.7071068, 0., -0.7071068}
>
> And I need this:
>
> K.Cross[K, g]
>
> to be 0 in accordance with a vector identity. The answer comes out to
> around 1.3 x 10^(-13), which is certainly close to 0 but not close
> enough for what I'm doing. I've tried various things like writing out
> the cross product explicitly without using the functions but the result
> is the same. And using N in various places doesn't seem to help either.
> What's the standard solution for this kind of thing?
>
> Thanks,
> Chris
>


Szabolcs Horvát

2008-03-20, 8:25 am

Chris Scullard wrote:
> Hi everyone,
>
> I wonder if I can get some opinions on the best way to deal with this
> precision issue I am having. I define the vectors:
>
> K = {111.5, 10.5, 1.5}
> g={-0.7071068, 0., -0.7071068}
>
> And I need this:
>
> K.Cross[K, g]
>
> to be 0 in accordance with a vector identity. The answer comes out to
> around 1.3 x 10^(-13), which is certainly close to 0 but not close
> enough for what I'm doing. I've tried various things like writing out
> the cross product explicitly without using the functions but the result
> is the same. And using N in various places doesn't seem to help either.
> What's the standard solution for this kind of thing?


I can't reproduce the problem with versions 5.2 or 6.

But you could consider using exact numbers or symbols instead of machine
precision numbers.

Also, avoid using identifiers starting with capital letters to prevent
collisions with built-in names. "K" is a built-in symbol.

Sseziwa Mukasa

2008-03-21, 4:49 am


On Mar 20, 2008, at 3:53 AM, Chris Scullard wrote:

> Hi everyone,
>
> I wonder if I can get some opinions on the best way to deal with this
> precision issue I am having. I define the vectors:
>
> K = {111.5, 10.5, 1.5}
> g={-0.7071068, 0., -0.7071068}
>
> And I need this:
>
> K.Cross[K, g]
>
> to be 0 in accordance with a vector identity. The answer comes out to
> around 1.3 x 10^(-13), which is certainly close to 0 but not close
> enough for what I'm doing. I've tried various things like writing out
> the cross product explicitly without using the functions but the
> result
> is the same. And using N in various places doesn't seem to help
> either.
> What's the standard solution for this kind of thing?


Chop

The larger issue is K and g don't have enough precision to satisfy
your identity, but g looks like it came from some trigonometric
identities:

g = {Cos[3 Pi/4],0,Cos[3 Pi/4]}

and K's entries can be expressed as rational numbers:

K = {223/2,21/2,3/2}

So that's your other option, don't take floating point approximations
to the entries in K and g.

In[215]:= {223/2,21/2,3/2}.Cross[{223/2,21/2,3/2},{Cos[3 Pi/4],0,Cos
[3 Pi/4]}]
Out[215]= 0

Regards,

Ssezi

Chris Scullard

2008-03-21, 4:49 am

That simple huh. Thanks Bob. And thanks everyone who replied.

Chris

Bob Hanlon wrote:[color=darkred]
> K = Rationalize[{111.5, 10.5, 1.5}, 0];
> g = Rationalize[{-0.7071068, 0., -0.7071068}, 0];
>
> K.Cross[K, g]
>
> 0
>
> K = {111.5`25, 10.5`25, 1.5`25};
> g = {-0.7071068`25, 0.`25, -0.7071068`25};
>
> Chop[K.Cross[K, g], 10^-18]
>
> 0
>
>
> Bob Hanlon
>
> ---- Chris Scullard <scullard@uchicago.edu> wrote:
>

Bob Hanlon

2008-03-21, 4:49 am

K = Rationalize[{111.5, 10.5, 1.5}, 0];
g = Rationalize[{-0.7071068, 0., -0.7071068}, 0];

K.Cross[K, g]

0

K = {111.5`25, 10.5`25, 1.5`25};
g = {-0.7071068`25, 0.`25, -0.7071068`25};

Chop[K.Cross[K, g], 10^-18]

0


Bob Hanlon

---- Chris Scullard <scullard@uchicago.edu> wrote:
> Hi everyone,
>
> I wonder if I can get some opinions on the best way to deal with this
> precision issue I am having. I define the vectors:
>
> K = {111.5, 10.5, 1.5}
> g={-0.7071068, 0., -0.7071068}
>
> And I need this:
>
> K.Cross[K, g]
>
> to be 0 in accordance with a vector identity. The answer comes out to
> around 1.3 x 10^(-13), which is certainly close to 0 but not close
> enough for what I'm doing. I've tried various things like writing out
> the cross product explicitly without using the functions but the result
> is the same. And using N in various places doesn't seem to help either.
> What's the standard solution for this kind of thing?
>
> Thanks,
> Chris
>



Jean-Marc Gulliet

2008-03-31, 6:44 am

Bob Hanlon wrote:

> K = Rationalize[{111.5, 10.5, 1.5}, 0];
> g = Rationalize[{-0.7071068, 0., -0.7071068}, 0];
>
> K.Cross[K, g]
>
> 0


<smip>

FWIW,

On my system v6.0.2 Mac Os X 1.5.2 64-bit,

g = Rationalize[{-0.7071068, 0., -0.7071068}]

returns the list unevaluated. To get a list of nice/exact numbers, I
must use *RootApproximant* (and of course K must be rationalized as
well). Only then I can get the value of zero for the dot-cross product.


In[1]:= $Version
K = Rationalize@{111.5, 10.5, 1.5}
g = RootApproximant@{-0.7071068, 0., -0.7071068}
K.Cross[K, g]

Out[1]= "6.0 for Mac OS X x86 (64-bit) (February 7, 2008)"

Out[2]= {223/2, 21/2, 3/2}

Out[3]= {-(1/Sqrt[2]), 0, -(1/Sqrt[2])}

Out[4]= 0

In[5]:= K = Rationalize@{111.5, 10.5, 1.5}
g = Rationalize@{-0.7071068, 0., -0.7071068}
K.Cross[K, g]

Out[5]= {223/2, 21/2, 3/2}

Out[6]= {-0.707107, 0, -0.707107}

Out[7]= 3.19744*10^-14

In[8]:= K = {111.5, 10.5, 1.5}
g = {-0.7071068, 0., -0.7071068}
K.Cross[K, g]

Out[8]= {111.5, 10.5, 1.5}

Out[9]= {-0.707107, 0., -0.707107}

Out[10]= 3.19744*10^-14


Best regards,
--
Jean-Marc


Bob Hanlon

2008-03-31, 6:44 am

As shown previously, use 0 as the tolerance (dx) to force rational.

g = Rationalize[{-0.7071068, 0., -0.7071068}, 0]



Bob Hanlon


---- Jean-Marc Gulliet <jeanmarc.gulliet@gmail.com> wrote:
> Bob Hanlon wrote:
>
>
> <smip>
>
> FWIW,
>
> On my system v6.0.2 Mac Os X 1.5.2 64-bit,
>
> g = Rationalize[{-0.7071068, 0., -0.7071068}]
>
> returns the list unevaluated. To get a list of nice/exact numbers, I
> must use *RootApproximant* (and of course K must be rationalized as
> well). Only then I can get the value of zero for the dot-cross product.
>
>
> In[1]:= $Version
> K = Rationalize@{111.5, 10.5, 1.5}
> g = RootApproximant@{-0.7071068, 0., -0.7071068}
> K.Cross[K, g]
>
> Out[1]= "6.0 for Mac OS X x86 (64-bit) (February 7, 2008)"
>
> Out[2]= {223/2, 21/2, 3/2}
>
> Out[3]= {-(1/Sqrt[2]), 0, -(1/Sqrt[2])}
>
> Out[4]= 0
>
> In[5]:= K = Rationalize@{111.5, 10.5, 1.5}
> g = Rationalize@{-0.7071068, 0., -0.7071068}
> K.Cross[K, g]
>
> Out[5]= {223/2, 21/2, 3/2}
>
> Out[6]= {-0.707107, 0, -0.707107}
>
> Out[7]= 3.19744*10^-14
>
> In[8]:= K = {111.5, 10.5, 1.5}
> g = {-0.7071068, 0., -0.7071068}
> K.Cross[K, g]
>
> Out[8]= {111.5, 10.5, 1.5}
>
> Out[9]= {-0.707107, 0., -0.707107}
>
> Out[10]= 3.19744*10^-14
>
>
> Best regards,
> --
> Jean-Marc
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com