[Scilab-users] Problems arising from truncation of %pi
Antoine Monmayrant
antoine.monmayrant at laas.fr
Wed Jan 6 09:22:11 CET 2021
Hello Frederico,
Like Christophe, I am not sure this has anything to do with the
implementation of sin().
It seems to be a known limitation of numerical calculations using
floating numbers.
In particular, even with en hypothetical ideal value of %pi, because of
the conversion to a double, %pi*1e15 is converted to the nearest
available double and this "shift" or error as compared to the ideal
infinitely-precise value is increasing with the size of your number.
Thus you make a x1e15 bigger error when using "%pi*1e15" than when using
"%pi".
As Christophe as said, there is not much you can do, apart from
resorting to symbolic calculation (what Alpha does).
You can see that by using nearfloat to get the distance between two doubles:
nearfloat("succ", %pi)-%pi
ans = 4.441D-16
nearfloat("succ", %pi*1e15)-%pi*1e15
ans = 0.5
As I said, the error on the sin() argument is getting x1e15 bigger!
I'm sorry if my explanation of floating point numbers is not really good.
This one was a revelation for me:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html .
It helped me understand why it's usually a good idea to use "reduced
units" for your calculations (ie keep everything close to 1 when possible).
I hope it helps,
Cheers,
Antoine
On 05/01/2021 09:19, Federico Miyara wrote:
>
> Dear all,
>
> In order to test the FFT on a periodic signal whose period is an exact
> submultiple of the FFT length I found a frequency fo satisfying this
> for the chosen sample rate and window length, and generated the
> following signal:
>
> x = sin(2*%pi*fo*t);
>
> where t is a time vector. This should be a perfectly periodic discrete
> signal but it isn't because the sin() function has a (virtually) exact
> period of pi, while %pi is exact to 16 digits only.
>
> For instance, we have (23 digits shown)
>
> --> sin(%pi)
> ans =
> 0.0000000000000001224647
>
> --> sin(1e10*%pi)
> ans =
> -0.0000022393627619559233
>
> --> sin(1e15*%pi)
> ans =
> -0.2362090532517409080526
>
> The Wolfram Alpha site yields the correct value 0 in all cases (using
> their own pi).
>
> Questions:
>
> 1) How is the sin() function extended to very large values of the
> argument? My first guess would be to compute a quarter cycle using
> Taylor and then extend it by symmetry and periodicity, but with which
> period? The best approximation available is 2*%pi. Or it is possible
> to use extended precision internally?
>
> 2) Is there a way to get a periodic discrete sine other than extending
> it periodically with the desired period?
> Wouldn't this create a slight glitch at the frontier between cycles?
>
> Regards,
>
> Federico Miyara
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
> Libre de virus. www.avast.com
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
>
>
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20210106/06d4ba0a/attachment.htm>
More information about the users
mailing list