Purebasic Precision

Everything else that doesn't fall into one of the other PB categories.
User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Purebasic Precision

Post by langinagel »

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.
We'll see about this.

Here some code in Purebasic:

Code: Select all

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: Select all

#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
https://www.doerpsoft.org

Boost. Work. Efficiency.
Bitblazer
Enthusiast
Enthusiast
Posts: 733
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Purebasic Precision

Post by Bitblazer »

webpage - discord chat links -> purebasic GPT4All
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Purebasic Precision

Post by davido »

@langinagel,

Please take a look at the links below:
viewtopic.php?p=458493#p458493
viewtopic.php?p=458533#p458533
Last edited by davido on Fri Sep 14, 2018 4:16 pm, edited 2 times in total.
DE AA EB
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Purebasic Precision

Post by STARGÅTE »

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: Select all

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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Purebasic Precision

Post by NicTheQuick »

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.
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.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Purebasic Precision

Post by Little John »

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.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Purebasic Precision

Post by jack »

@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.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Purebasic Precision

Post by Little John »

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.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Purebasic Precision

Post by jack »

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).
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Purebasic Precision

Post by Little John »

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.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Purebasic Precision

Post by jack »

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.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Purebasic Precision

Post by Little John »

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.
Post Reply