## subtract two angle

### subtract two angle

Hi all,

I have a strange result when I subtract two angle in radians!!!!

example :
Code: Select all
`axe = Geom::Vector3d.new(-0.999782867743517, -0.0208378829670674, 0.0)up = Geom::Vector3d.new(0.00335712544250249, -0.161071856847676, 0.98693697196954)axe.normalize!up.normalize!# (90 deg) = 1.5707963267948966 in rad#(Math::PI/2.0) = 90.0 in degrees, 1.5707963267949 in radians# angle between up-axe = 90.0 in degrees, 1.5707963267949 in radiansangle = (Math::PI/2.0) + (up.angle_between axe)puts angle.radians  #=> 180 degrees, 3.14159265358979 radians GOOD !!!angle = (Math::PI/2.0) - (up.angle_between axe)puts angle.radians  #=> 1.27222187258541e-014 BAD,  2.22044604925031e-016 in radians BAD  `

can you help me ?
Last edited by thomthom on Tue Feb 14, 2012 9:52 am, edited 1 time in total.
Reason: Wrapped the code sample in a [code] block
macgile

Posts: 33
Joined: Sat Jan 19, 2008 10:59 am

### Re: subtract two angle

What version Sketchup and build number, are you running ???

The angle_between method was bugged in early v7 releases.

But it was later fixed.

Dan Rathbun
Top SketchUcator

Posts: 4074
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 8
SketchUp use: education

### Re: subtract two angle

thank's Dan Rathbun

i have sketchup 8 !!!

it's not angle between, but the subtraction.
because the calculation of the angle is good.
macgile

Posts: 33
Joined: Sat Jan 19, 2008 10:59 am

### Re: subtract two angle

Why not use 90.degrees etc and return the angle.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 ?
Code: Select all
`axe = Geom::Vector3d.new(-0.999782867743517, -0.0208378829670674, 0.0)Vector3d(-0.999783, -0.0208379, 0)axe.normalize!Vector3d(-0.999783, -0.0208379, 0)up = Geom::Vector3d.new(0.00335712544250249, -0.161071856847676, 0.98693697196954)Vector3d(0.00335713, -0.161072, 0.986937)up.normalize!Vector3d(0.00335713, -0.161072, 0.986937)90.degrees1.5707963267949Math::PI/2.01.5707963267949up.angle_between(axe)1.5707963267949### So far ALL the same ???angle = (Math::PI/2.0) - (up.angle_between(axe))2.22044604925031e-016### Which is a very tiny number that's ALMOST zeroThis is a a simple tolerance issue...angle==0false### BUT to test for tiny values use something like:### a 'rounding' method - here to 6dp.angle=(angle*10**6).round.to_f/10**6angle==0true### This will then trap for these tiny inaccuracies...  `

...
Last edited by thomthom on Tue Feb 14, 2012 9:52 am, edited 1 time in total.
Reason: Wrapped the code sample in a [code] block
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: subtract two angle

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

Dan Rathbun
Top SketchUcator

Posts: 4074
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 8
SketchUp use: education

### Re: subtract two angle

TIG wrote:Why not use 90.degrees etc and return the angle.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 ?
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*10**6).round.to_f/10**6
angle==0
true
### This will then trap for these tiny inaccuracies...
...
macgile

Posts: 33
Joined: Sat Jan 19, 2008 10:59 am

### Re: subtract two angle

Dan Rathbun
Top SketchUcator

Posts: 4074
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 8
SketchUp use: education

### Re: subtract two angle

Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17625
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

Vertex Tools for SketchUp. Take control over each vertex with this vertex editor for SketchUp.