• AVR Freaks

Helpful ReplyHot!Very strange SIN and COS results *FIXED*

Author
Skydec
Junior Member
  • Total Posts : 99
  • Reward points : 0
  • Joined: 2010/04/20 07:28:12
  • Location: 0
  • Status: offline
2020/03/31 05:06:22 (permalink)
0

Very strange SIN and COS results *FIXED*

I have a problem I can't find a solution to.
I use some functions with a cos and sin function
For example I have a function : sh = sinf(h);
h is of type float
when I hardcode h = 1.570796325;
sh will be 1 (that's correct)
When I run my code h is nearly 90 degrees (there are some conversions that makes it not exactly 90, but that is not a problem)
first I convert to radians by this function h = h * M_PI / 180.0;
Than the result is: 1.250 (or another result > 1 it's not always the same)
1.250 is an invalid result as the sin function cannot be greater then 1
I tried many things, cast it as a double, convert it to a double and use sin instead of sinf
Everytime I get 1.250 as a result.
Other values give sometimes correct values, sometimes incorrect values.
I'm really out of options.
What could be the cause of this?
post edited by Skydec - 2020/03/31 07:53:51
#1
Skydec
Junior Member
  • Total Posts : 99
  • Reward points : 0
  • Joined: 2010/04/20 07:28:12
  • Location: 0
  • Status: offline
Re: Very strange SIN and COS results 2020/03/31 05:41:11 (permalink)
0
Update
I have written "my own" sin and cos function using Taylor series and then the results are correct.
I think Taylor series is not the fastest way to calculate the sin and cos function, so I prefer to go back to the Math's functions.
However I think it proves there is something wrong with the math.h's version.
By the way, I'm using compiler XC32 v2.3 with optimalisation 3 (I see there is also version 2.4 available, so I will download and install v2.4)
#2
andersm
Super Member
  • Total Posts : 2796
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: Very strange SIN and COS results 2020/03/31 06:55:50 (permalink) ☄ Helpfulby Skydec 2020/03/31 07:53:20
5 (2)
When experiencing floating-point weirdness with XC32, the first thing always to check is if you're using 32- or 64-bit doubles, since that has been a constant source of bugs. The whole option has now been deprecated, and in XC32 2.40 the default was changed to use 64-bit doubles.
#3
Skydec
Junior Member
  • Total Posts : 99
  • Reward points : 0
  • Joined: 2010/04/20 07:28:12
  • Location: 0
  • Status: offline
Re: Very strange SIN and COS results 2020/03/31 07:08:18 (permalink)
0
Well maybe you are pointing in the right direcetion.
I use doubles and long doubles all together in my code, but in this function I only use long double and float.
The SIN and COS functions are done as floats (I used sinf and cosf) result goes into float as well..
 
I will give 2.4 a try, but getting rid of the 32bit floats, what does this to the speed of the math functions?
#4
friesen
Super Member
  • Total Posts : 2123
  • Reward points : 0
  • Joined: 2008/05/08 05:23:35
  • Location: Indiana, USA
  • Status: offline
Re: Very strange SIN and COS results 2020/03/31 07:34:01 (permalink)
0
Are you sure the MC lib expects radians?

Erik Friesen
#5
Skydec
Junior Member
  • Total Posts : 99
  • Reward points : 0
  • Joined: 2010/04/20 07:28:12
  • Location: 0
  • Status: offline
Re: Very strange SIN and COS results 2020/03/31 07:53:12 (permalink)
0
friesen
Are you sure the MC lib expects radians?


It doesn't mather if you use degrees or radians, whatever you input, cos and sin should never be greater than 1 even if you mixup degrees and radians.
And yes it's based on gcc, so it expects radians.
 
andersm
When experiencing floating-point weirdness with XC32, the first thing always to check is if you're using 32- or 64-bit doubles, since that has been a constant source of bugs. The whole option has now been deprecated, and in XC32 2.40 the default was changed to use 64-bit doubles.


2.4 fixed the issue! thanks... now I can use the standard sinf and cosf functions.
 
#6
1and0
Access is Denied
  • Total Posts : 10779
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Very strange SIN and COS results 2020/03/31 08:12:19 (permalink)
0
I recall reading there were bugs with floating point operation (sine uses float) in XC32 couple years ago. Unfortunately, I don't remember any details now. I would think it is fixed by now, but who knows. Glad you find the fix.
#7
nigelwright7557
Super Member
  • Total Posts : 421
  • Reward points : 0
  • Joined: 2006/11/06 08:15:51
  • Location: 0
  • Status: online
Re: Very strange SIN and COS results 2020/03/31 09:11:54 (permalink)
0
I remember many many years ago having to use Sin and Cos in an assembler program.
It didnt have to be massively accurate so I just created a table of 90 items between 0 and 255 to represent 0 to 1.
Obviously that gives you Sin 0-90 degrees so just make a simple subroutine to convert to 90-180, 180-270 and 270-359 degrees. Cos is just 90 degrees out from Sin so easily fixed.
Worked a treat and very fast with integers.
 
#8
andersm
Super Member
  • Total Posts : 2796
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: Very strange SIN and COS results 2020/03/31 09:45:00 (permalink)
5 (1)
SkydecAnd yes it's based on gcc, so it expects radians.

GCC doesn't supply a math library (nor a C library), but the C standard does specify that the trigonometric functions use radians.
 
The XC32 short-double problems could manifest in several ways, but one common issue was when people were trying to output floating-point numbers using the printf family. Since printf is a variadic function, floats are promoted to doubles, and if the compiler and library don't agree on what size a double is, you get all kinds of interesting results.
#9
moser
Super Member
  • Total Posts : 557
  • Reward points : 0
  • Joined: 2015/06/16 02:53:47
  • Location: Germany
  • Status: offline
Re: Very strange SIN and COS results 2020/04/01 00:26:40 (permalink)
5 (1)
1and0
I recall reading there were bugs with floating point operation (sine uses float) in XC32 couple years ago. Unfortunately, I don't remember any details now. I would think it is fixed by now, but who knows. Glad you find the fix.

You can have a look at thread https://www.microchip.com/forums/FindPost/1130997 and the links in my post there.
I believe that the XC32 v2.30 is not comparable to that old XC32 v1.41. But nevertheless this could give you a vague impression about what kind of issues are causing all these troubles.
#10
Skydec
Junior Member
  • Total Posts : 99
  • Reward points : 0
  • Joined: 2010/04/20 07:28:12
  • Location: 0
  • Status: offline
Re: Very strange SIN and COS results 2020/04/01 02:13:03 (permalink)
0
andersm
 
The XC32 short-double problems could manifest in several ways, but one common issue was when people were trying to output floating-point numbers using the printf family. Since printf is a variadic function, floats are promoted to doubles, and if the compiler and library don't agree on what size a double is, you get all kinds of interesting results.




That was what going to my mind as well, I had this kind of trouble before with printf, so I  tested the result first if it was greater then 1 and then print the result, to be sure it was not a printf issue, but it was not...
I think we can conclude that the floating point operations were not very stable in XC32, hopefully it has been resolved with version 2.4, so far I don't have any issues with floating point operations compiled with v2.4
#11
Jump to:
© 2020 APG vNext Commercial Version 4.5