Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:23 17 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2 - scientific format

     Page 1 of 2    
Author Message
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 01:50pm 22 Feb 2021
Copy link to clipboard 
Print this post

One of my sanity inducing mindfull-ness type things I do is look for number sequences.  (See https://oeis.org/ if you want maximum geek).

Currently hunting for numbers of the form:




... which are also square numbers. Where the formula represents a k-agon based pyramid (stacking cannon balls problem).

The following code works fine:

Function sum(x)
   k=8  'an octagonal based pyramid
   sum = ((0.5*k-1)*x^2)-((-0.5*k+2)*x)
End Function

Function is_square$(i)
   If Sqr(i) = Int(Sqr(i))  Then
       is_square$="true"
   Else
       is_square$="false"
   End If
End Function

Sub square()
For x = 1 To 10000000
   If is_square$(sum(x)) = "true" Then
       Print x
       Print #1, x
   End If
Next x
End Sub

square()


but once the numbers get large, they are scaled to scientific notation. Thus...

2
32
450
6272
87362
1.2168e+06


This confuses my students, most of which have not seen this notation before.  I naively thought that if I saved the output to file, that this would expand the number.  Nope.

I know I can add format$(x,"%.20g") to force these larger numbers into expanded form -- but that looks even more like weird code to my students.

Q: Is there any way to either scientific notation and just display the full number?  OPTION SCIBASE OFF for example?  

Or is there some other CMM2/MMBasic-fu that can make scientific notation disappear?  Would even be happy if this was just enabled when output is directed to a file?


As a second supplementary Q:

Every number is more than an order of magnitude bigger than the previous - this means that beyond the sixth term the rate of discovery grinds to a halt.  In Python (dont hate me) I have a library that I can load that displays a "spinner" and/or progress bar to show that the code hasn't hung / died. Whilst I can simulate this in the CMM2, the act of "Printing" itself slows the code down.

Other than printing a "..." every 1000 iterations or so, any clever ways to show that code is still running?

Cheers
Nim
Edited 2021-02-22 23:54 by Nimue
Entropy is not what it used to be
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10591
Posted: 01:55pm 22 Feb 2021
Copy link to clipboard 
Print this post

print str$(x,15,0)
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 01:55pm 22 Feb 2021
Copy link to clipboard 
Print this post

with limitations, if you convert the number to integer you get the significant digits:


> dim a%
> a%=1.2168e+06
> ? a%
1216800

> a%=2^63
> ?a%
-9223372036854775808


etc...

does that help?
Edited 2021-02-22 23:57 by CaptainBoing
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 01:59pm 22 Feb 2021
Copy link to clipboard 
Print this post

  matherp said  print str$(x,15,0)


Perfect -- that is "easier" for them to understand -- does what it says on the tin.

Thank you.
N
Edited 2021-02-22 23:59 by Nimue
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 02:00pm 22 Feb 2021
Copy link to clipboard 
Print this post

  CaptainBoing said  dim a%
> a%=1.2168e+06
>
limitations, if you convert the number to integer you get the significant digits:


> dim a%
> a%=1.2168e+06
> ? a%
1216800

> a%=2^63
> ?a%
-9223372036854775808


etc...

does that help?



Nice - yes it does ---

Is "integer" math quicker too? As in:

For x% = 1 to 100000
next x%


or is the % ignored in the above?

Nim
Edited 2021-02-23 00:04 by Nimue
Entropy is not what it used to be
 
NPHighview

Senior Member

Joined: 02/09/2020
Location: United States
Posts: 213
Posted: 04:30pm 22 Feb 2021
Copy link to clipboard 
Print this post

...and don't hesitate to teach them scientific (and engineering) notation.  They'll use it forever more.
- Steve
Live in the Future. It's Just Starting Now!
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 04:37pm 22 Feb 2021
Copy link to clipboard 
Print this post

  NPHighview said  and don't hesitate to teach them scientific (and engineering) notation.  They'll use it forever more.
- Steve


Agree -- but most are only 10-12 years old.  Standard form appears in GCSE (for a couple of marks).  

They struggle with significant figures as it is ;-)

edit >>> Odd things happening when i use "quote" function in the forum... adding a URL link??

N
Edited 2021-02-23 02:38 by Nimue
Entropy is not what it used to be
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 05:01pm 23 Feb 2021
Copy link to clipboard 
Print this post

On the bog standard MM (which I use in reasonable amounts) Integer maths is very much faster than floats, but I don't know how much so on those beasts with hardware FPUs. I suspect there is still a healthy gap... perhaps some tests...


timer=0
for n%=1 to 1000000:next
?timer

timer=0
For m!=1 to 1000000:next
?timer



and following on from NP's tip above, engineering notation is a very good thing to teach. check these out

EDIT: just did the tests myself, given that its on a 48MHz PIC32, there isn't much in it between ints and floats... pleasantly surprised:

Int   23892
Float 26683

just three seconds in it, over 1M iterations... I have new found respect for floats on the humble MM. So not "very much" really - a bit faster

p.s. yes, just recently I have noticed rogue URL tags being inserted in quoted posts too
Edited 2021-02-24 03:38 by CaptainBoing
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 05:46pm 23 Feb 2021
Copy link to clipboard 
Print this post

  Nimue said  Odd things happening when i use "quote" function in the forum... adding a URL link??


  CaptainBoing said  p.s. yes, just recently I have noticed rogue URL tags being inserted in quoted posts too


I emailed Glenn about this several days ago. He replied and I sent him some examples, so he's aware of it (said he had fixed something else which might have broken it).
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4147
Posted: 06:48pm 23 Feb 2021
Copy link to clipboard 
Print this post

Oh, Glenn may be getting too many reports - I also emailed.

John
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 06:51pm 23 Feb 2021
Copy link to clipboard 
Print this post

Cap'n's code for F4:
4667
6816

for CMM2:
688.069
722.549
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 622
Posted: 11:29pm 23 Feb 2021
Copy link to clipboard 
Print this post

now try

timer=0
for n%=1 to 1000000:next
i% = i% + 1
?timer

timer=0
For m!=1 to 1000000:next
x! = x! + 1.0
?timer
Edited 2021-02-24 09:31 by zeitfest
 
TimD
Newbie

Joined: 23/02/2021
Location: United Kingdom
Posts: 31
Posted: 12:09am 25 Feb 2021
Copy link to clipboard 
Print this post

  Nimue said  
Other than printing a "..." every 1000 iterations or so, any clever ways to show that code is still running?

Not sure how clever it is, but an efficient method may be to use SETTICK to call a subroutine at regular intervals; the subroutine can then display your current value of x.

Best regards,
Tim.
 
Plasmamac

Guru

Joined: 31/01/2019
Location: Germany
Posts: 596
Posted: 01:35am 25 Feb 2021
Copy link to clipboard 
Print this post

maybe use the VBI ?

mode x,x,x,int
Plasma
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 12:22pm 25 Feb 2021
Copy link to clipboard 
Print this post

  TimD said  Not sure how clever it is, but an efficient method may be to use SETTICK to call a subroutine at regular intervals; the subroutine can then display your current value of x.
Best regards,
Tim.


Nice  - investigating now

N
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 12:24pm 25 Feb 2021
Copy link to clipboard 
Print this post

  Plasmamac said  maybe use the VBI ?

mode x,x,x,int


OOhh - that's interesting.

Both replies use interrupts -- off to play.

N
Entropy is not what it used to be
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 622
Posted: 01:29pm 25 Feb 2021
Copy link to clipboard 
Print this post

  zeitfest said  now try

timer=0
for n%=1 to 1000000:next
i% = i% + 1
?timer

timer=0
For m!=1 to 1000000:next
x! = x! + 1.0
?timer


What times show on these ? Anyone ?
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 01:54pm 25 Feb 2021
Copy link to clipboard 
Print this post

  zeitfest said  
  zeitfest said  now try

timer=0
for n%=1 to 1000000:next
i% = i% + 1
?timer

timer=0
For m!=1 to 1000000:next
x! = x! + 1.0
?timer


What times show on these ? Anyone ?


CMM2:

3568.879
4387.374

N
Entropy is not what it used to be
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 03:17pm 25 Feb 2021
Copy link to clipboard 
Print this post

  Nimue said  
  zeitfest said  
  zeitfest said  now try

timer=0
for n%=1 to 1000000:next
i% = i% + 1
?timer

timer=0
For m!=1 to 1000000:next
x! = x! + 1.0
?timer


What times show on these ? Anyone ?


CMM2:

3568.879
4387.374

N

Something wrong there. With the whole series copied into the prompt, and a dummy command after the final ?timer, I get this:

> timer=0
> for n%=1 to 1000000:next
> ?timer
658.13
>
> timer=0
> For m!=1 to 1000000:next
> ?timer
695.027
>
> timer=0
> for n%=1 to 1000000:next
> i% = i% + 1
> ?timer
678.432
>
> timer=0
> For m!=1 to 1000000:next
> x! = x! + 1.0
> ?timer
711.618

With this

timer=0
for n%=1 to 1000000:next
?timer

timer=0
For m!=1 to 1000000:next
?timer

timer=0
for n%=1 to 1000000:next
i% = i% + 1
?timer

timer=0
For m!=1 to 1000000:next
x! = x! + 1.0
?timer
x! = x! + 1.0

About 20ms added for "i% = i% + 1" and 16ms for "x! = x! + 1.0".
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 03:43pm 25 Feb 2021
Copy link to clipboard 
Print this post

The "issue" with mine was:

your code....

timer=0
for n%=1 to 1000000:next
i% = i% + 1
?timer


Inadvertently I figured the i%=i%+1 needed to be inside the for loop.

my code:

timer=0
for n%=1 to 1000000
  i% = i% + 1
next
?timer

Entropy is not what it used to be
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025