Velocity Reviews > Does C have function to get angle?

# Does C have function to get angle?

Vol
Guest
Posts: n/a

 11-23-2005
I think 'atan' can get the angle but it is not the four quadrant angle.
Is there any function that i can get the angle from -pi to pi? or I
have to use some if ... else?
I know in Matlab, we use 'atan2( )'

Also, is there any function to get the round value, similar with floor
and ceil, such like:

round(3.1) = 3
round(3.6) = 4

thanks..

Vol

Christopher Benson-Manica
Guest
Posts: n/a

 11-23-2005
Vol <(E-Mail Removed)> wrote:

> I think 'atan' can get the angle but it is not the four quadrant angle.
> Is there any function that i can get the angle from -pi to pi? or I
> have to use some if ... else?
> I know in Matlab, we use 'atan2()'

atan2() also exists in C, although I can't say whether it's identical
to the MatLab function you're referencing.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

William Hughes
Guest
Posts: n/a

 11-23-2005

Vol wrote:
> I think 'atan' can get the angle but it is not the four quadrant angle.
> Is there any function that i can get the angle from -pi to pi? or I
> have to use some if ... else?
> I know in Matlab, we use 'atan2( )'
>
> Also, is there any function to get the round value, similar with floor
> and ceil, such like:
>
> round(3.1) = 3
> round(3.6) = 4
>
> thanks..
>
> Vol

atan2(y,x) exists in C (and Matlab and Octave and Python and ...
I think all of these simply call the C function).
Take care because atan2(0,0) is undefined, some
implementations will return 0, some will crash (I think the DS2K does
a Morris Dance).

If you need to round x use floor(x + 0.5). (If you care about why
this is not perfect you already know why)

- William Hughes

DevarajA
Guest
Posts: n/a

 11-23-2005
William Hughes ha scritto:
> Vol wrote:
>
>>I think 'atan' can get the angle but it is not the four quadrant angle.
>>Is there any function that i can get the angle from -pi to pi? or I
>>have to use some if ... else?
>>I know in Matlab, we use 'atan2( )'
>>
>>Also, is there any function to get the round value, similar with floor
>>and ceil, such like:
>>
>>round(3.1) = 3
>>round(3.6) = 4
>>
>>thanks..
>>
>>Vol

>
>
>
> atan2(y,x) exists in C (and Matlab and Octave and Python and ...
> I think all of these simply call the C function).
> Take care because atan2(0,0) is undefined, some
> implementations will return 0, some will crash (I think the DS2K does
> a Morris Dance).
>
> If you need to round x use floor(x + 0.5). (If you care about why
> this is not perfect you already know why)
>

Can you tell me why?

--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org

William Hughes
Guest
Posts: n/a

 11-23-2005

DevarajA wrote:
> William Hughes ha scritto:
> > Vol wrote:
> >
> >>I think 'atan' can get the angle but it is not the four quadrant angle.
> >>Is there any function that i can get the angle from -pi to pi? or I
> >>have to use some if ... else?
> >>I know in Matlab, we use 'atan2( )'
> >>
> >>Also, is there any function to get the round value, similar with floor
> >>and ceil, such like:
> >>
> >>round(3.1) = 3
> >>round(3.6) = 4
> >>
> >>thanks..
> >>
> >>Vol

> >
> >
> >
> > atan2(y,x) exists in C (and Matlab and Octave and Python and ...
> > I think all of these simply call the C function).
> > Take care because atan2(0,0) is undefined, some
> > implementations will return 0, some will crash (I think the DS2K does
> > a Morris Dance).
> >
> > If you need to round x use floor(x + 0.5). (If you care about why
> > this is not perfect you already know why)
> >

>
> Can you tell me why?
>

It is possible if x is less than 0.5 (but very close) for
x+0.5 to be greater than or equal to 1 in which case
floor(x+0.5) will be 1 rather than 0.

Also, it is not in general possible to represent a decimal
fraction exactly, so an implementation may represent

0.4999999999999999

as a floating point number greater than or equal to 0.5
in which case

floor(0.4999999999999999 + 0.5)

will be 1 rather than 0.

Question. Do you care?

-William Hughes

DevarajA
Guest
Posts: n/a

 11-23-2005
William Hughes ha scritto:
[cut]
>>

>
>
> It is possible if x is less than 0.5 (but very close) for
> x+0.5 to be greater than or equal to 1 in which case
> floor(x+0.5) will be 1 rather than 0.
>
> Also, it is not in general possible to represent a decimal
> fraction exactly, so an implementation may represent
>
> 0.4999999999999999
>
> as a floating point number greater than or equal to 0.5
> in which case
>
> floor(0.4999999999999999 + 0.5)
>
> will be 1 rather than 0.

Ok, thank you.

>
> Question. Do you care?
>

Yes. I like precision.

--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org

Randy Howard
Guest
Posts: n/a

 11-23-2005
DevarajA wrote
(in article <4384b5a2\$0\$7329\$(E-Mail Removed)>):

>> Question. Do you care?
>>

>
> Yes. I like precision.

Then you definitely need to read this:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

William Hughes
Guest
Posts: n/a

 11-23-2005

DevarajA wrote:
> William Hughes ha scritto:
> [cut]
> >>

> >
> >
> > It is possible if x is less than 0.5 (but very close) for
> > x+0.5 to be greater than or equal to 1 in which case
> > floor(x+0.5) will be 1 rather than 0.
> >
> > Also, it is not in general possible to represent a decimal
> > fraction exactly, so an implementation may represent
> >
> > 0.4999999999999999
> >
> > as a floating point number greater than or equal to 0.5
> > in which case
> >
> > floor(0.4999999999999999 + 0.5)
> >
> > will be 1 rather than 0.

>
> Ok, thank you.
>
> >
> > Question. Do you care?
> >

>
> Yes. I like precision.

How much?

Joe Wright
Guest
Posts: n/a

 11-23-2005
William Hughes wrote:
> DevarajA wrote:
>
>>William Hughes ha scritto:
>>
>>>Vol wrote:
>>>
>>>
>>>>I think 'atan' can get the angle but it is not the four quadrant angle.
>>>>Is there any function that i can get the angle from -pi to pi? or I
>>>>have to use some if ... else?
>>>>I know in Matlab, we use 'atan2( )'
>>>>
>>>>Also, is there any function to get the round value, similar with floor
>>>>and ceil, such like:
>>>>
>>>>round(3.1) = 3
>>>>round(3.6) = 4
>>>>
>>>>thanks..
>>>>
>>>>Vol
>>>
>>>
>>>
>>>atan2(y,x) exists in C (and Matlab and Octave and Python and ...
>>>I think all of these simply call the C function).
>>>Take care because atan2(0,0) is undefined, some
>>>implementations will return 0, some will crash (I think the DS2K does
>>>a Morris Dance).
>>>
>>>If you need to round x use floor(x + 0.5). (If you care about why
>>>this is not perfect you already know why)
>>>

>>
>>Can you tell me why?
>>

>
>
> It is possible if x is less than 0.5 (but very close) for
> x+0.5 to be greater than or equal to 1 in which case
> floor(x+0.5) will be 1 rather than 0.
>
> Also, it is not in general possible to represent a decimal
> fraction exactly, so an implementation may represent
>
> 0.4999999999999999
>
> as a floating point number greater than or equal to 0.5
> in which case
>
> floor(0.4999999999999999 + 0.5)
>
> will be 1 rather than 0.
>
> Question. Do you care?
>
> -William Hughes
>

Your example is a little flawed. First, 0.5 is exactly representable in
binary floating point. Your constant, 0.4999999999999999 looks like
4.9999999999999989e-01 on my machine. Less than 0.5 of course. If you're
curious, the binary of the 64-bit double is..

00111111110111111111111111111111111111111111111111 11111111111110

...The binary of 0.5 is..

00111111111000000000000000000000000000000000000000 00000000000000

...Close, no cigar.

If you add one more '9' to the constant it will "round up" to 0.5 and
your example will be true. As it is, floor(0.4999999999999999 + 0.5)
yields 0.0, not 1.0 here at my house.

Question. Do you care?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

William Hughes
Guest
Posts: n/a

 11-23-2005

Joe Wright wrote:
> William Hughes wrote:
> > DevarajA wrote:
> >
> >>William Hughes ha scritto:
> >>
> >>>Vol wrote:
> >>>
> >>>
> >>>>I think 'atan' can get the angle but it is not the four quadrant angle.
> >>>>Is there any function that i can get the angle from -pi to pi? or I
> >>>>have to use some if ... else?
> >>>>I know in Matlab, we use 'atan2( )'
> >>>>
> >>>>Also, is there any function to get the round value, similar with floor
> >>>>and ceil, such like:
> >>>>
> >>>>round(3.1) = 3
> >>>>round(3.6) = 4
> >>>>
> >>>>thanks..
> >>>>
> >>>>Vol
> >>>
> >>>
> >>>
> >>>atan2(y,x) exists in C (and Matlab and Octave and Python and ...
> >>>I think all of these simply call the C function).
> >>>Take care because atan2(0,0) is undefined, some
> >>>implementations will return 0, some will crash (I think the DS2K does
> >>>a Morris Dance).
> >>>
> >>>If you need to round x use floor(x + 0.5). (If you care about why
> >>>this is not perfect you already know why)
> >>>
> >>
> >>Can you tell me why?
> >>

> >
> >
> > It is possible if x is less than 0.5 (but very close) for
> > x+0.5 to be greater than or equal to 1 in which case
> > floor(x+0.5) will be 1 rather than 0.
> >
> > Also, it is not in general possible to represent a decimal
> > fraction exactly, so an implementation may represent
> >
> > 0.4999999999999999
> >
> > as a floating point number greater than or equal to 0.5
> > in which case
> >
> > floor(0.4999999999999999 + 0.5)
> >
> > will be 1 rather than 0.
> >
> > Question. Do you care?
> >
> > -William Hughes
> >

> Your example is a little flawed. First, 0.5 is exactly representable in
> binary floating point. Your constant, 0.4999999999999999 looks like
> 4.9999999999999989e-01 on my machine. Less than 0.5 of course. If you're
> curious, the binary of the 64-bit double is..
>
> 00111111110111111111111111111111111111111111111111 11111111111110
>
> ..The binary of 0.5 is..
>
> 00111111111000000000000000000000000000000000000000 00000000000000
>
> ..Close, no cigar.
>
> If you add one more '9' to the constant it will "round up" to 0.5 and
> your example will be true. As it is, floor(0.4999999999999999 + 0.5)
> yields 0.0, not 1.0 here at my house.

And this is a counterexample to my statement

so an implementation may represent

0.4999999999999999

as a floating point number greater than or equal to 0.5

how?