Subtract two angle
-
Hi all,
I have a strange result when I subtract two angle in radians!!!!
example :
<span class="syntaxdefault">axe </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.new(-</span><span class="syntaxdefault">0.999782867743517</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.0208378829670674</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0.0</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">up </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">0.00335712544250249</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.161071856847676</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0.98693697196954</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">axe</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normalize</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault">up</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normalize</span><span class="syntaxkeyword">!<br /></span><span class="syntaxcomment"># (90 deg) = 1.5707963267948966 in rad<br />#(Math;;PI/2.0) = 90.0 in degrees, 1.5707963267949 in radians<br /><br /># angle between up-axe = 90.0 in degrees, 1.5707963267949 in radians<br /></span><span class="syntaxdefault">angle </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Math</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">PI</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2.0</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">up</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">angle_between axe</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">puts angle</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">radians </span><span class="syntaxcomment">#=> 180 degrees, 3.14159265358979 radians GOOD !!!<br /><br /></span><span class="syntaxdefault">angle </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Math</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">PI</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2.0</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">up</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">angle_between axe</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">puts angle</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">radians </span><span class="syntaxcomment">#=> 1.27222187258541e-014 BAD, 2.22044604925031e-016 in radians BAD </span><span class="syntaxdefault"></span>
can you help me ?
-
What version Sketchup and build number, are you running ???
The
angle_between
method was bugged in early v7 releases.But it was later fixed.
-
thank's Dan Rathbun
i have sketchup 8 !!!
it's not angle between, but the subtraction.
because the calculation of the angle is good. -
It's a tiny tiny wee little smidgen Floating Point Error.
Round your numbers off to a reasonable number of decimal places.
Your inputting double precision numbers, but Sketchup's internal tolerance for geometry is 0.001 inch.
You can use the Length class to test if two numbers are equal within Sketchup's tolerance, even though they are angles: (your not changing the objects, your creating two new
Length
objects for comparison.)(Math::PI/2.0).to_l == up.angle_between(axe).to_l
true
-
@tig said:
Why not use
90.degrees
etc and return theangle.radians
to see what it is in degrees too... skip all of the PI and radian confusion...=====> I tested with 90.degrees, but I thought it was the conversion that was bad
Why are you making your axe/up vectors so complex ?
Your figures are off too...
axe = Geom::Vector3d.new(-0.999782867743517, -0.0208378829670674, 0.0)=====> because it is real vectors returned with axe.inspect
I was given a real example.Which is a very tiny number that's ALMOST zero
This is a a simple tolerance issue...
======>Tolerance has importance here because it is cumulative.
BUT to test for tiny values use something like:
a 'rounding' method - here to 6dp.
angle=(angle*106).round.to_f/106
angle==0
trueThis will then trap for these tiny inaccuracies...
...
-
Wikipedia: Floating_point : Accuracy_problems
-
Why not use
90.degrees
etc and return theangle.radians
to see what it is in degrees too... skip all of the PI and radian confusion...
Why are you making your axe/up vectors so complex ?
Your figures are off too...<span class="syntaxdefault">axe </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.new(-</span><span class="syntaxdefault">0.999782867743517</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.0208378829670674</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0.0</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">(-</span><span class="syntaxdefault">0.999783</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.0208379</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">axe</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normalize</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">(-</span><span class="syntaxdefault">0.999783</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.0208379</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">up </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">0.00335712544250249</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.161071856847676</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0.98693697196954</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0.00335713</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.161072</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0.986937</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">up</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normalize</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0.00335713</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">0.161072</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0.986937</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">90.degrees<br />1.5707963267949<br />Math</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">PI</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2.0<br />1.5707963267949<br />up</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">angle_between</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">axe</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">1.5707963267949<br /></span><span class="syntaxcomment">### So far ALL the same ???<br /></span><span class="syntaxdefault">angle </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Math</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">PI</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">2.0</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">up</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">angle_between</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">axe</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">2.22044604925031e-016<br /></span><span class="syntaxcomment">### Which is a very tiny number that's ALMOST zero<br /></span><span class="syntaxdefault">This is a a simple tolerance issue</span><span class="syntaxkeyword">...<br /></span><span class="syntaxdefault">angle</span><span class="syntaxkeyword">==</span><span class="syntaxdefault">0<br />false<br /></span><span class="syntaxcomment">### BUT to test for tiny values use something like;<br />### a 'rounding' method - here to 6dp.<br /></span><span class="syntaxdefault">angle</span><span class="syntaxkeyword">=(</span><span class="syntaxdefault">angle</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">10</span><span class="syntaxkeyword">**</span><span class="syntaxdefault">6</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">round</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_f</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">10</span><span class="syntaxkeyword">**</span><span class="syntaxdefault">6<br />angle</span><span class="syntaxkeyword">==</span><span class="syntaxdefault">0<br />true<br /></span><span class="syntaxcomment">### This will then trap for these tiny inaccuracies... </span><span class="syntaxdefault"></span>
...
-
What Every Programmer Should Know About Floating-Point Arithmetic
A.K.A. "Why don’t my numbers add up?"
Advertisement