Author Topic: v1.5 128 bit (and beyond) math *** COMPILER ERROR ***  (Read 60021 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: v1.5 128 bit (and beyond) math
« Reply #120 on: May 30, 2021, 11:23:04 pm »
The amount of use it will receive will be next to nil, I'm almost certain.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: v1.5 128 bit (and beyond) math
« Reply #122 on: May 31, 2021, 12:30:16 am »
@bplus I don't mean in general. I mean in QB64.
Shuwatch!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #123 on: May 31, 2021, 03:30:32 am »
I would like to see Spriggs do something with regular numbers before he predicts the futility of really big ones.
You're not done when it works, you're done when it's right.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: v1.5 128 bit (and beyond) math
« Reply #124 on: May 31, 2021, 03:54:41 am »
Alright well I sure do hope I see some 128 bit numbers being used in that NASA code and can't wait to see what colorful little demo we get next.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #125 on: May 31, 2021, 10:37:45 am »
Alright well I sure do hope I see some 128 bit numbers being used in that NASA code and can't wait to see what colorful little demo we get next.

Well this thread has been all over the place, did we visit NASA too?

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: v1.5 128 bit (and beyond) math
« Reply #126 on: May 31, 2021, 06:06:31 pm »
I think he was talking about me.

I don't do work for NASA, but some of the grad students and research projects I code for, use data FROM NASA, observatories, and universities.

Unfortunately, none of those programs are in QB64 (yet), and I also sign a confidentiality (non-disclosure) agreement before working on projects.

I could work on some examples, like using the Planck Constant of 6.62607015×10^-34 ( or .0000000000000000000000000000000000662607015) or the Planck Unit (1.75 × 10^-63). The Planck Constant I've used in research on black-body radiation and the Planck Unit with the Hubble Constant for research into the expansion of the universe. (see image on how QB64 puts garbage in when printing the Planck Constant using the "PRINT USING" that goes past the 34th decimal place.

Anyway, I would like to use QB64, and that's where my (and bplus') string math routines come into play (until a language developer puts in 128bit math or functionality for large number math).

The functionality could be a special pre-compiler processing tag, so it does not effect the normal use of the language.

  [ You are not allowed to view this attachment ]  




Well this thread has been all over the place, did we visit NASA too?
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: v1.5 128 bit (and beyond) math
« Reply #127 on: June 01, 2021, 12:38:29 am »
@SpriggsySpriggs, @bplus

Here is an example of why it is important to increase the size of the accuracy or precision of numbers.

The program should produce a standard deviation of 10.34945. That's all.

However, when I extent the PRINT USING to accommodate the possibility of a number larger than this (say, 15 decimal places) you will see that I get garbage after the "945" using DOUBLE, SINGLE and _FLOAT.

This is an everyday example of the need for using large numbers, and QB64 fails at it. Which is why I and others have asked for changes to include large number math routines or the implementation of 128bit math processing.

It is not that it will not be used that often. In fact, it can be used for simple tasks that generally will produce numbers greater than the mantissa that restricts the precision of a result of a simple standard deviation. And if this is screwed up, even more uses will provide invalid results, and anyone looking for accuracy is just not going to get it.

It's not a matter of nice to have. It is something needed for many programming tasks.

Here is the code, followed by my results:

Code:
Code: QB64: [Select]
  1. REM Finding Standard Deviation
  2.  
  3. DIM SDEV_D AS DOUBLE
  4. DIM SDEV_S AS SINGLE
  5. DIM SDEV_F AS _FLOAT
  6.  
  7. SUM = 0
  8.  
  9. FOR K = 1 TO 10
  10.     READ N(K)
  11.     SUM = SUM + N(K)
  12.  
  13. AVE = SUM / 10
  14.  
  15. SUM = 0
  16. FOR K = 1 TO 10
  17.     SUM = SUM + (N(K) - AVE) ^ 2
  18.  
  19. SDEV = SQR(SUM / 9)
  20. SDEV_D = SQR(SUM / 9)
  21. SDEV_S = SQR(SUM / 9)
  22. SDEV_F = SQR(SUM / 9)
  23.  
  24. PRINT "The average is "; AVE: PRINT
  25. PRINT "The standard deviation for SDEV without 'PRINT USING' is "; SDEV
  26. PRINT USING "The standard deviation for SDEV is  ###.###############"; SDEV
  27. PRINT USING "The standard deviation for SDEV_D is ###.###############"; SDEV_D
  28. PRINT USING "The standard deviation for SDEV_S is ###.###############"; SDEV_S
  29. PRINT USING "The standard deviation for SDEV_F is ###.###############"; SDEV_F
  30.  
  31. REM DATA Statement
  32. DATA 72,84,96,88,91,75,79,100,76,99

Results:
  [ You are not allowed to view this attachment ]  
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: v1.5 128 bit (and beyond) math
« Reply #128 on: June 01, 2021, 01:02:32 am »
There’s a lot of big num libraries out there for C.  If you guys really need such precision, you should look a few up and write wrappers and DECLARE LIBRARY them for use.  As I posted above, boost offers up to 1024-bit precision.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #129 on: June 01, 2021, 07:54:49 am »
@SpriggsySpriggs, @bplus

Here is an example of why it is important to increase the size of the accuracy or precision of numbers.

The program should produce a standard deviation of 10.34945. That's all.

However, when I extent the PRINT USING to accommodate the possibility of a number larger than this (say, 15 decimal places) you will see that I get garbage after the "945" using DOUBLE, SINGLE and _FLOAT.
after the second loop SUM = 964 so SQR(SUM / 9) is going to vary depending on the precision, it's not going to be exactly 10.34945 except perhaps for single
bplus or STxAxTIC could probably write a pre-parser that would convert a basic program's  math expressions into a program that would call string math functions

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: QB64 v1.5 Feature Request - Fellippe's interpreter option
« Reply #130 on: June 01, 2021, 08:41:26 am »
Is that what you guys are looking for, an Immediate Window?
So you can test a snippet of code.

I'll just chime in and say, I would LOVE an immediate Window, that works like the one in Excel or Word VBA (even better would be one where you can scroll up and see the whole session).
It's just a great tool & option when developing.
I'll note that the immediate window in the old Visual Studio (say around 2008) was not as useful, as it didn't let you just click on it and start typing.
That behavior is specifically in MS Office VBA, and is what makes it so useful.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: v1.5 128 bit (and beyond) math
« Reply #131 on: June 01, 2021, 12:17:44 pm »
There’s a lot of big num libraries out there for C.  If you guys really need such precision, you should look a few up and write wrappers and DECLARE LIBRARY them for use.  As I posted above, boost offers up to 1024-bit precision.

Precisely. This can be easily sought out and a wrapper be made. This sort of reminds me of how WinAPI handles 64 bit numbers in compilers that don't support 64 bit natively. You just use a struct. Anyways, looking forward to seeing some wrapper made here.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #132 on: June 01, 2021, 12:22:53 pm »
@SpriggsySpriggs, @bplus

Here is an example of why it is important to increase the size of the accuracy or precision of numbers.

The program should produce a standard deviation of 10.34945. That's all.

However, when I extent the PRINT USING to accommodate the possibility of a number larger than this (say, 15 decimal places) you will see that I get garbage after the "945" using DOUBLE, SINGLE and _FLOAT.

This is an everyday example of the need for using large numbers, and QB64 fails at it. Which is why I and others have asked for changes to include large number math routines or the implementation of 128bit math processing.

It is not that it will not be used that often. In fact, it can be used for simple tasks that generally will produce numbers greater than the mantissa that restricts the precision of a result of a simple standard deviation. And if this is screwed up, even more uses will provide invalid results, and anyone looking for accuracy is just not going to get it.

It's not a matter of nice to have. It is something needed for many programming tasks.

Here is the code, followed by my results:

Code:
Code: QB64: [Select]
  1. REM Finding Standard Deviation
  2.  
  3. DIM SDEV_D AS DOUBLE
  4. DIM SDEV_S AS SINGLE
  5. DIM SDEV_F AS _FLOAT
  6.  
  7. SUM = 0
  8.  
  9. FOR K = 1 TO 10
  10.     READ N(K)
  11.     SUM = SUM + N(K)
  12.  
  13. AVE = SUM / 10
  14.  
  15. SUM = 0
  16. FOR K = 1 TO 10
  17.     SUM = SUM + (N(K) - AVE) ^ 2
  18.  
  19. SDEV = SQR(SUM / 9)
  20. SDEV_D = SQR(SUM / 9)
  21. SDEV_S = SQR(SUM / 9)
  22. SDEV_F = SQR(SUM / 9)
  23.  
  24. PRINT "The average is "; AVE: PRINT
  25. PRINT "The standard deviation for SDEV without 'PRINT USING' is "; SDEV
  26. PRINT USING "The standard deviation for SDEV is  ###.###############"; SDEV
  27. PRINT USING "The standard deviation for SDEV_D is ###.###############"; SDEV_D
  28. PRINT USING "The standard deviation for SDEV_S is ###.###############"; SDEV_S
  29. PRINT USING "The standard deviation for SDEV_F is ###.###############"; SDEV_F
  30.  
  31. REM DATA Statement
  32. DATA 72,84,96,88,91,75,79,100,76,99

Results:
  [ You are not allowed to view this attachment ]

Well here is nice little test of String Math, guess I better get going with that SQR routine. I have to ask what precision answer you'd like or are expecting. The data suggests that it's all integers with precision of +-.5. Not being a statistician, I can only guess that double precision would be fine for Standard Deviation on Integer Data.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: v1.5 128 bit (and beyond) math
« Reply #133 on: June 01, 2021, 12:24:04 pm »
You are correct, but there are two issues here -
1. If I have to print out the results and I have no idea how large or small the results will be, QB64 adds garbage at the end of the number AND
2. This is a simple standard deviation. We use this with numbers that are more than 25 significant digits, or have more than 25 decimal places (or both) and the results are just not there.

But QB64 has more problems than many interpreters that run on mobile devices, whose hardware isn't as robust as our desktops.

Here is part of a program I wrote many years ago after I had to appear before an inquiry board to explain why my results, or the system's results, were inaccurate. Here I just include the most simplistic tests of processor/language number accuracy tests, that use the most basic physics figures known to even layman. I also included a comparison to a run I did in TechBASIC on my iPad.

You'll see that the iPad, even though both contain garbage in their numbers that do affect the results of any calculation in it, gets closer to the right answer than QB64 does. In fact, I have no clue why !E+308 produces garbage results (not even close to the right result) and the iPad gets it 100% correct!

Note, the program is exactly the same on the iPad. The iPad's TechBASIC uses DOUBLE FLOAT for all the variables. In QB64, I use both DOUBLE and _FLOAT and get the same results. (TechBASIC is written in C, so there is no reason why QB64 can't get this simple math correct!)

Here is the program followed by screen shots of both a QB64 run (in 64bit) on a Linux, and the TechBASIC run. The code is 100 percent the same.

This is a problem that must be fixed on the compiler side.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1200, 800, 32)
  2.  
  3. X = 2 ^ 52 - 1
  4. Z = 2 ^ 53 - 1
  5. W = X + X
  6.  
  7. Y = 2 * 10 ^ 30
  8. Y1 = 2D * 10D ^ 30D
  9. B = 2D0 * 10 ^ 30D
  10. C = 2D0 * 10D ^ 30D
  11. D = 2D0 * EXP(30 * ln(10D0))
  12. E = 2D0 * EXP(30D * ln(10D0))
  13.  
  14. A = 1E+308
  15. A$ = "1E+308"
  16. LY = ((186000 * 60 ^ 2) * 24) * 365.25
  17.  
  18. Y$ = STR$(Y)
  19.  
  20. PRINT "The maximum absolute value 2^52     = ";: PRINT USING "#,###,###,###,###,###"; X
  21. PRINT USING "Add #,###,###,###,###,### to itself = #,###,###,###,###,###"; X, W
  22. PRINT "The maximum absolute value 2^53     = ";: PRINT USING "#,###,###,###,###,###"; Z
  23.  
  24. PRINT A$;: PRINT USING "   =   #######################"; A
  25. PRINT A$;: PRINT USING "   = ####,###,###,###,###,###"; A
  26.  
  27. PRINT "As an exponent, the Sun's Mass.        = "; Y$
  28. PRINT "The mass of the Sun 2*10^30 ";: PRINT USING "           = #.#####################^"; Y
  29. PRINT "The mass of the Sun 2d*10d^30d ";: PRINT USING "        = #.#####################^"; Y1
  30. PRINT USING "The answer of B using all _FLOAT = #.#####################^"; B
  31. PRINT USING "The answer of C using all _FLOAT = #.#####################^"; C
  32. PRINT USING "The answer of D using all _FLOAT = #.######################"; D
  33. PRINT USING "The answer of E using all _FLOAT = #.######################"; E
  34.  
  35. PRINT USING "Light Year = ###,###,###,###,###,### miles"; LY
  36.  

Here is the QB64 Run:
  [ You are not allowed to view this attachment ]  


Here is the TechBASIC run on the iPad:
  [ You are not allowed to view this attachment ]  



after the second loop SUM = 964 so SQR(SUM / 9) is going to vary depending on the precision, it's not going to be exactly 10.34945 except perhaps for single
bplus or STxAxTIC could probably write a pre-parser that would convert a basic program's  math expressions into a program that would call string math functions
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: v1.5 128 bit (and beyond) math
« Reply #134 on: June 01, 2021, 12:43:07 pm »
One problem, George, is it looks like your program doesn't calculate standard deviation correctly. I checked against a standard deviation calculator and your numbers don't return 10.34945. I looked up a C++ example to calculate standard deviation and mine matches the online deviation calculator.

Code: QB64: [Select]
  1.  
  2. Print Using "#.#############"; calculateSD
  3.  
  4. Dats:
  5. Data 72,84,96,88,91,75,79,100,76,99
  6.  
  7.  
  8. Function calculateSD## ()
  9.     Dim As _Float sum, mean, standardDeviation
  10.     Dim As _Float dat
  11.     sum = 0.0: standardDeviation = 0.0
  12.     Dim As Long i
  13.     Restore Dats
  14.     For i = 1 To 10
  15.         Read dat
  16.         sum = sum + dat
  17.     Next
  18.     mean = sum / 10
  19.     Restore Dats
  20.     For i = 1 To 10
  21.         Read dat
  22.         standardDeviation = standardDeviation + ((dat - mean) ^ 2)
  23.     Next
  24.     calculateSD = Sqr(standardDeviation / 10)

You can check that code against this link:
https://www.calculator.net/standard-deviation-calculator.html?numberinputs=72%2C84%2C96%2C88%2C91%2C75%2C79%2C100%2C76%2C99&ctype=p&x=33&y=10

My return:                              9.8183501669069
That online calculator's return: 9.8183501669069
Shuwatch!