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

0 Members and 1 Guest are viewing this topic.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: v1.5 128 bit (and beyond) math
« Reply #135 on: June 01, 2021, 01:00:35 pm »
@SpriggsySpriggs - If you chose SAMPLE (which I thought I said it was in my post, but I used the word SIMPLE instead) your calculator produces the same results I get.

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
____________________________________________________________________
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 OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #136 on: June 01, 2021, 01:15:28 pm »
I'm no computer scientist, nor an astrophysicist. I'm a farmer that flunked one algebra class and learned to use a little trig at my lathe and milling machine, and I like writing utility programs for an old role playing hobby. QB64 is excellent for what it does, namely rescuing Qbasic from obscurity.

That said, I agree with George. In working with my space flight utility, I learned early on that I would not put my own flesh in a ship running on QB64. Big numbers generated "issues" that necessitated the use of relative coordinate systems to keep the numbers as small and local as possible. Even so, Qbasic couldn't handle even the most rudimentary version of that program, so I'm still happy.


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #137 on: June 01, 2021, 01:33:43 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

I get a perfect match between his program and yours, just not at 10.... but at the 9.... that you show

PS I added the print of mean in function.
« Last Edit: June 01, 2021, 01:34:54 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: v1.5 128 bit (and beyond) math
« Reply #138 on: June 01, 2021, 02:11:44 pm »
Why does the techbasic on the ipad print the wrong answer? 

((186000 * 60 ^ 2) * 24) * 365.25 = 5869713600000 (at least according to both google and my windows calculator, which is what I lazily used to check results), which is what QB64 gives us.  The screen shot of your ipad is showing the result as 5869713489920.

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

I must be missing something here, as it seems QB64 is giving us the correct answers while the mobile device screenshot you posted isn't.
« Last Edit: June 01, 2021, 02:14:34 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: v1.5 128 bit (and beyond) math
« Reply #139 on: June 01, 2021, 02:32:34 pm »
Another question I have is one this line: E = 2D0 * EXP(30D * ln(10D0))

There’s no array called ln in your program, so ln(10) is going to be 0.  Is that what you expect here?  Or are you looking for LOG(10)?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #140 on: June 01, 2021, 02:35:03 pm »
Oh the code in reply #127 is wrong, some fixes and it comes in on double and _Float because intermediary variables are _float:
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. Dim sum As _Float '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< intermediaries have to be same or better precision
  7. Dim n(1 To 10) As _Float
  8. sum = 0
  9.  
  10. For K = 1 To 10
  11.     Read n(K)
  12.     sum = sum + n(K)
  13.  
  14. ave = sum / 10
  15.  
  16. sum = 0
  17. For K = 1 To 10
  18.     sum = sum + (n(K) - ave) ^ 2
  19.  
  20. SDEV = Sqr(sum / 10) ' divide by 10 not 9
  21. SDEV_D = Sqr(sum / 10)
  22. SDEV_S = Sqr(sum / 10)
  23. SDEV_F = Sqr(sum / 10)
  24.  
  25. Print "The average is "; ave: Print
  26. Print "The standard deviation for SDEV without 'PRINT USING' is "; SDEV
  27. Print Using "The standard deviation for SDEV is  ###.###############"; SDEV
  28. Print Using "The standard deviation for SDEV_D is ###.###############"; SDEV_D
  29. Print Using "The standard deviation for SDEV_S is ###.###############"; SDEV_S
  30. Print Using "The standard deviation for SDEV_F is ###.###############"; SDEV_F
  31.  
  32. Rem DATA Statement
  33. Data 72,84,96,88,91,75,79,100,76,99
  34.  
  35.  

Sorry I thought Spriggsy was quoting George code, guess that function was his own in QB64 that he was comparing to the Internet calc.
« Last Edit: June 01, 2021, 02:42:55 pm by bplus »

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: v1.5 128 bit (and beyond) math
« Reply #141 on: June 01, 2021, 02:45:34 pm »
I left the code exactly the same as it was in TechBASIC.

Without the ln it seems to not produce a result on the iPad. Since I am working on an iOS app right now, I will test what I just did in QB64 later. But with it, it corrects the issue of memory corruption and produces the right value. I haven't looked into why, but it just does.



Another question I have is one this line: E = 2D0 * EXP(30D * ln(10D0))

There’s no array called ln in your program, so ln(10) is going to be 0.  Is that what you expect here?  Or are you looking for LOG(10)?
____________________________________________________________________
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 #142 on: June 01, 2021, 02:50:54 pm »
Oh the code in reply #127 is wrong, some fixes and it comes in on double and _Float because intermediary variables are _float:
Sorry I thought Spriggsy was quoting George code, guess that function was his own in QB64 that he was comparing to the Internet calc.

@bplus Right, exactly. I was comparing the converted C++ code I found to the internet calculator
Shuwatch!

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: v1.5 128 bit (and beyond) math
« Reply #143 on: June 01, 2021, 02:52:07 pm »
I raised that very issue with TechBASIC's developer, Mike Westerfield.

I was told that the ARM processor on mobile devices wasn't as robust as those on desktops.

And since all math is done via registers on the CPU (in an area that is the math co-processor), it winds up being a hardware limitation that, unless you create your own bit-math (or string math) process, all hardware, from mainframes down to iPhones and iPads and even single board computers, have precision issues.

Saying that, with properly constructed code within compilers and interpreters, large number math can be precise.

Back in 1977 I worked on enhancing OS/HASP at Rockland Research Institute (now Nathan S. Kline Institute) so that the hardware limits for researchers and scientists were less of an issue.

The same can be put into QB64, with hardly any noticeable performance issues.


Why does the techbasic on the ipad print the wrong answer? 

((186000 * 60 ^ 2) * 24) * 365.25 = 5869713600000 (at least according to both google and my windows calculator, which is what I lazily used to check results), which is what QB64 gives us.  The screen shot of your ipad is showing the result as 5869713489920.

I must be missing something here, as it seems QB64 is giving us the correct answers while the mobile device screenshot you posted isn't.
____________________________________________________________________
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 #144 on: June 01, 2021, 02:53:18 pm »
I left the code exactly the same as it was in TechBASIC.

Without the ln it seems to not produce a result on the iPad. Since I am working on an iOS app right now, I will test what I just did in QB64 later. But with it, it corrects the issue of memory corruption and produces the right value. I haven't looked into why, but it just does.

Different languages, different commands.  Seems like TechBasic uses ln, while QB64 uses LOG.

Code won’t be the same unless you take the effort to convert to the proper language commands.  We use SQR, C uses sqrt...  You can’t just copy a formula directly from one into the other and expect it to always work.  You have to convert to proper syntax.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: v1.5 128 bit (and beyond) math
« Reply #145 on: June 01, 2021, 03:00:50 pm »
Both my result and yours are correct. It is a matter of which Standard Deviation is being performed. I chose SAMPLE as it illustrated my example.

However, for those who do not understand the difference, I guess I need to clear up the difference between "POPULATION" vs "SAMPLE" in regards to Standard Deviation:

They are:

Quote
The population standard deviation is a parameter, which is a fixed value calculated from every individual in the population.

A sample standard deviation is a statistic. This means that it is calculated from only some of the individuals in a population. Since the sample standard deviation depends upon the sample, it has greater variability. Thus the standard deviation of the sample is greater than that of the population.

 The formulas to calculate both of these standard deviations are nearly identical:

    Calculate the mean.
    Subtract the mean from each value to obtain deviations from the mean.
    Square each of the deviations.
    Add together all of these squared deviations.

Now the calculation of these standard deviations differs:

    If we are calculating the population standard deviation, then we divide by n, the number of data values.
    If we are calculating the sample standard deviation, then we divide by n -1, one less than the number of data values.

The final step, in either of the two cases that we are considering, is to take the square root of the quotient from the previous step.

The larger the value of n is, the closer that the population and sample standard deviations will be.


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
____________________________________________________________________
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 #146 on: June 01, 2021, 03:05:08 pm »
@SMcNeill

I tried LOG in both and it does not work.

It seems that TechBASIC assigns LONG to the array, based on the value that is in it. However, when I take it away, the calculation does not work due to the exponents. I don't know why.

But removing that in QB64 (ie: D = 2D0 * EXP(30 * 10D0) ) produces the same results, so it is mute. It works the same either way.

EDIT: I take that back - there is a difference, but which is correct? Also, what is the % in front of some of my numbers?

Different languages, different commands.  Seems like TechBasic uses ln, while QB64 uses LOG.

Code won’t be the same unless you take the effort to convert to the proper language commands.  We use SQR, C uses sqrt...  You can’t just copy a formula directly from one into the other and expect it to always work.  You have to convert to proper syntax.
« Last Edit: June 01, 2021, 03:10:17 pm by George McGinn »
____________________________________________________________________
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 bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #147 on: June 01, 2021, 03:17:17 pm »
Both my result and yours are correct. It is a matter of which Standard Deviation is being performed. I chose SAMPLE as it illustrated my example.

However, for those who do not understand the difference, I guess I need to clear up the difference between "POPULATION" vs "SAMPLE" in regards to Standard Deviation:

They are:

 The formulas to calculate both of these standard deviations are nearly identical:

    Calculate the mean.
    Subtract the mean from each value to obtain deviations from the mean.
    Square each of the deviations.
    Add together all of these squared deviations.

Now the calculation of these standard deviations differs:

    If we are calculating the population standard deviation, then we divide by n, the number of data values.
    If we are calculating the sample standard deviation, then we divide by n -1, one less than the number of data values.

The final step, in either of the two cases that we are considering, is to take the square root of the quotient from the previous step.

The larger the value of n is, the closer that the population and sample standard deviations will be.

Ah something that has confused me since day one when trying to write a standard deviation code for data,
sometimes it's divide by n others by n-1 and also sometimes its with the SQR and sometimes without.

Never-the-less when making a point about precision calculations, make sure that intermediary variables hold same precision at least as you desire in the final result. Did you see difference that made? For me, I couldn't match Spriggsy's results until I changed the intermediary variables, the ones used to aid in finding the final results.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #148 on: June 01, 2021, 03:23:12 pm »
QB64 uses base 10 for LOG, ln usually implies base e, there is a note how to convert QB64 base 10 LOG to ln in the Wiki under LOG.

Dang got it backwards, QB64 LOG is natural (base e) what most people call ln(x) and the conversion to base 10 is in the note.
http://qb64.org/wiki/LOG
Code: QB64: [Select]
  1. Print (Exp(1)) 'e
  2. Print Log(Exp(1)) '1
  3.  
« Last Edit: June 01, 2021, 03:50:04 pm by bplus »

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: v1.5 128 bit (and beyond) math
« Reply #149 on: June 01, 2021, 10:21:10 pm »
@madscijr

Regarding your reply #130 (this thread)

A number of forum members would welcome the feature of an immediate window, but despite request for same it would seem it is not going to happen with the developers.

If you refer to the following replies from me:-

https://www.qb64.org/forum/index.php?topic=3947.30  see reply #34

https://www.qb64.org/forum/index.php?topic=1839.msg110823#msg110823 see reply #2

https://www.qb64.org/forum/index.php?topic=2276.msg116118#msg116118 see reply #8

I had experimented with dual instances of QB64 to simulate TRON-TROFF, breakpoints, watchpoints etc (from very old BASIC days) - and in principal an "immediate window" within the program (i.e. second instance BLOATED program) may, with care, give useful capability comparable to a true intermediate window. It is "messy" but with programming care it can be automated into programs (i.e. BLOAT original program (in instance #1), auto-insert TRON-TROFF flags, breakpoints, watchpoints, immediate window sort-of capability, logs, parsing original and replace with alternative code, etc. - and then UNBLOATING to recreate the original program after debugging/developing (in instance #2))

Of course, a cheap and nasty solution is to simply open up a "new instance" of QB64 to quickly test a snippet of code but of course it may not be so convenient in many cases because certain events/values may be required beforehand.

I have never used any version of MS Office or accessories etc but in MS PDS 7.1 (30 year old BASIC) it still has very important and useful features (such as immediate window, etc) which despite calls is still lacking in QB64.

For me, I may apply this technique (in the BLOATED program) to in  "parallel" with the math code of instance #1, simultaneously check all calculations etc with 128 bit computations and auto-insert relevant comments generated by instance #2 into the instance #1 (UNBLOATED) program.