# PureBasic Forum

 It is currently Sun Feb 17, 2019 1:22 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 12 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Purebasic PrecisionPosted: Thu Sep 13, 2018 10:46 pm
 Enthusiast

Joined: Fri Jan 28, 2005 11:53 pm
Posts: 113
Location: Otterloh
Howdy,
I came across a method to check the mathematical precision of the math library. The c-stdlib should be the partner of comparison.
Requirement:
Produce Exponents of 10. Multiply them with Pi. Take the sinus of the result.
sin (Pi) = 0 (half a revolution), any integer multiples of Pi must be 0 again.

Here some code in Purebasic:
Code:
EnableExplicit
Define.d dWinkel1, dWinkel2, dPi

Define.i iNummer, index

dPi = 3.1415926535897932384;626433832795028841971693993751058209749445

For index = 0 To  30
Debug Str(index) + " Index"
dWinkel1 = Pow(10, index)
Debug StrD(dWinkel1) + " Ergebnis"
dWinkel1 =  dPi * dWinkel1

dWinkel2 = Sin( dWinkel1 )
Debug StrD(dWinkel2) + " Sinus"

Debug Chr(13)
Next

And here the C-code for comparison:
Code:
#include <math.h>
#include <stdio.h>
int main() {
double dWinkel;
double dWinkel2;
double dIndex;                          //
const double dPi = 3.1415926535897932384626433832795028841971693993751058209749445;
int index;

for (index =0; index < 30; index++) {

dIndex  = (double) index;
printf("%f. Index \n", dIndex);

dWinkel = pow (10.00000 , dIndex);
printf("%f. Ergebnis \n", dWinkel);

dWinkel =  dPi * dWinkel;
dWinkel2 = sin( dWinkel );

printf("%f. Sinus \n", dWinkel2);
printf("\n");

}
return(0);
}

And now the result:
Purebasic C
0 0.000000. Index
1 1.000000. Ergebnis
0 0.000000. Sinus

1 1.000000. Index
10 10.000000. Ergebnis
0 -0.000000. Sinus

2 2.000000. Index
100 100.000000. Ergebnis
0 0.000000. Sinus

3 3.000000. Index
1000 1000.000000. Ergebnis
0 -0.000000. Sinus

4 4.000000. Index
10000 10000.000000. Ergebnis
0 -0.000000. Sinus

5 5.000000. Index
100000 100000.000000. Ergebnis
0 -0.000000. Sinus

6 6.000000. Index
1000000 1000000.000000. Ergebnis
-0.0000000002 -0.000000. Sinus

7 7.000000. Index
10000000 10000000.000000. Ergebnis
0.0000000006 0.000000. Sinus

8 8.000000. Index
100000000 100000000.000000. Ergebnis
-0.0000000391 -0.000000. Sinus

9 9.000000. Index
1000000000 1000000000.000000. Ergebnis
-0.0000000332 -0.000000. Sinus

10 10.000000. Index
10000000000 10000000000.000000. Ergebnis
-0.0000022393 -0.000002. Sinus

11 11.000000. Index
100000000000 100000000000.000000. Ergebnis
-0.0000147638 -0.000015. Sinus

12 12.000000. Index
1000000000000 1000000000000.000000. Ergebnis
-0.0002697086 -0.000270. Sinus

13 13.000000. Index
10000000000000 10000000000000.000000. Ergebnis
-0.0026970827 -0.002697. Sinus

14 14.000000. Index
100000000000000 100000000000000.000000. Ergebnis
-0.0113456165 -0.011346. Sinus

15 15.000000. Index
1000000000000000 1000000000000000.000000. Ergebnis
-0.2362051232 -0.236209. Sinus

16 16.000000. Index
1E+016 10000000000000000.000000. Ergebnis
-0.3751753994 -0.375213. Sinus

17 17.000000. Index
1E+017 100000000000000000.000000. Ergebnis
-0.84818399 -0.847970. Sinus

18 18.000000. Index
1E+018 1000000000000000000.000000. Ergebnis
-0.6447503601 -0.641653. Sinus

19 19.000000. Index
1E+019 10000000000000000000.000000. Ergebnis
3,14159265358979E+019 0.746337. Sinus

20 20.000000. Index
1E+020 100000000000000000000.000000. Ergebnis
3,14159265358979E+020 -0.394071. Sinus

21 21.000000. Index
1E+021 1000000000000000000000.000000. Ergebnis
3,14159265358979E+021 -0.580805. Sinus

22 22.000000. Index
1E+022 10000000000000000000000.000000. Ergebnis
3,14159265358979E+022 -0.688675. Sinus

23 23.000000. Index
1E+023 99999999999999991611392.000000. Ergebnis
3,14159265358979E+023 -0.796516. Sinus

24 24.000000. Index
1E+024 999999999999999983222784.000000. Ergebnis
3,14159265358979E+024 0.930141. Sinus

25 25.000000. Index
1E+025 10000000000000000905969664.000000. Ergebnis
3,14159265358979E+025 0.994809. Sinus

26 26.000000. Index
1E+026 100000000000000004764729344.000000. Ergebnis
3,14159265358979E+026 0.957898. Sinus

27 27.000000. Index
1E+027 1000000000000000013287555072.000000. Ergebnis
3,14159265358979E+027 0.789794. Sinus

28 28.000000. Index
1E+028 9999999999999999583119736832.000000. Ergebnis
3,14159265358979E+028 -0.492936. Sinus

29 29.000000. Index
1E+029 99999999999999991433150857216.000000. Ergebnis
3,14159265358979E+029 -0.999143. Sinus

30 30.000000. Index
1E+030 1000000000000000019884624838656.000000. Ergebnis
3,14159265358979E+030 0.872051. Sinus

Interpretation:
StrD works fine as very precise output.
With 1E16 the difference appears without rounding.
With 1E19 Purebasics sinus-function quits while C can provide an output near 0.

Anyone trying to work with Purebasic in high-number science? Do we need a better library?
A discussion might help...
Nice evening
LN

_________________
Stoh wis, kiek wiet, rög di.

ANSI X3.60-1978 "For minimal BASIC" !!!
ANSI X3.113-1987 "Programming Languages Full BASIC" !!!
ISO xxxxx "Purebasic Programming Language" - draft ???

Top

 Post subject: Re: Purebasic PrecisionPosted: Thu Sep 13, 2018 11:43 pm
 Enthusiast

Joined: Mon Apr 10, 2017 6:17 pm
Posts: 159
Location: Germany
Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 6:12 am

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1624
Location: Uttoxeter, UK
@langinagel,

https://www.purebasic.fr/english/viewtopic.php?p=458493#p458493
https://www.purebasic.fr/english/viewtopic.php?p=458533#p458533

_________________
DE AA EB

Last edited by davido on Fri Sep 14, 2018 4:16 pm, edited 2 times in total.

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 7:00 am

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1184
Location: Germany, Glienicke
langinagel wrote:
With 1E19 Purebasics sinus-function quits while C can provide an output near 0.

The output at C is also complete nonsense that you can not use, since Sin is always between -1 and 1.

Vom PB side, you can use Mod(dWinkel1,dPi) to prevent the divagation, but the fluctuation comes from the double precission.
Code:
EnableExplicit
Define.d dWinkel1, dWinkel2, dPi

Define.i iNummer, index

dPi = 3.1415926535897932384;626433832795028841971693993751058209749445

For index = 0 To  30
Debug Str(index) + " Index"
dWinkel1 = Pow(10, index)
Debug StrD(dWinkel1) + " Ergebnis"
dWinkel1 =  dPi * dWinkel1

dWinkel2 = Sin( Mod(dWinkel1,dPi) )
Debug StrD(dWinkel2) + " Sinus"

Debug Chr(13)
Next

_________________

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 10:47 am
 Enthusiast

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 336
Location: Germany, Saarbrücken
There is no difference in PB and C because both simply use what the processor offers them. The difference comes with more complexity of the formulas or if you are using vector arithmetic (SIMD) because then it depends on the compiler which registers were used and in which order the steps will be processed. Most C compilers can optimize your code much more than PB does it. There are a lot of CPU instructions available for vector arithmetic like SSE2, SSE3, SSSE3, SSE4, SSE4a, AVX and more which PB does not care about. Using them can affect the results of your calculations.

_________________
Electronics, Crazy & Interesting Stuff, all that with text, image and sound? Click here!

The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 4:42 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3414
Location: Berlin, Germany
It would be really helpful if PureBasic offered an extended-precision floating-point data type (size 10 bytes) for constants and variables. E.g. PowerBasic supported this already in its "ancient" DOS versions.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 5:19 pm

Joined: Fri Apr 25, 2003 11:10 pm
Posts: 1185
@Little John
I think that it would be more trouble than it's worth, the FPU seems to be deprecated, SSE seems to be the rage.
now if PB had operator and function overloading, then you could use softfloat or other math library as if it were native to PB.

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 6:51 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3414
Location: Berlin, Germany
jack wrote:
@Little John
I think that it would be more trouble than it's worth,

It would not cause any trouble, but would be worth a lot. On the customer list of PowerBasic, there are e.g. NASA, astronomers, and other researchers. They were probably not on that list if PowerBasic did not support extended-precision floating-point.

jack wrote:
the FPU seems to be deprecated, SSE seems to be the rage.

Extended-precision floating-point is part of the IEEE 754 standard. In modern processors, the x87 commands are built-in in the CPU. These processors can addittionaly handle SSE2 commands.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 7:46 pm

Joined: Fri Apr 25, 2003 11:10 pm
Posts: 1185
honestly, I don't think any serious scientist is using a deprecated language like PowerBasic, they might have in the past (maybe 20+ years ago).

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 7:56 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3414
Location: Berlin, Germany
jack wrote:
honestly, I don't think any serious scientist is using a deprecated language like PowerBasic, they might have in the past (maybe 20+ years ago).

PowerBasic came into trouble when its creator Bob Zale died. But that is not the point here.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 8:08 pm

Joined: Fri Apr 25, 2003 11:10 pm
Posts: 1185
who said anything about former PowerBasic's owner?
the product, PowerBasic, is old and discontinued, any serious scientist would long ago have moved on to something else.
besides, the extended precision format is not very efficient in modern OS, with (12)or16-byte alignment, there's a lot of wasted resources, you might as well use a 128-bit math lib.

Top

 Post subject: Re: Purebasic PrecisionPosted: Fri Sep 14, 2018 8:16 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3414
Location: Berlin, Germany
jack wrote:
who said anything about former PowerBasic's owner?
Me. Because the reason for PowerBasic being discontinued was his death, but not lack of quality of the product.

jack wrote:
the product, PowerBasic, is old and discontinued, any serious scientist would long ago have moved on to something else.

Apart from "discontinued", this is just your personal guess. Anyway, as I already tried to point out, this is not a discussion about PowerBasic. I only had mentioned it as an example.

My point is, that it would be a big advantage for PureBasic if it had built-in support for higher precision floating-point data types then it currently has. 128 bits would be OK with me, too.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 12 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 4 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite