nickgammon / BigNumber

BigNumber library for the Arduino
MIT License
87 stars 22 forks source link

Weird locking #10

Closed mariavd closed 5 years ago

mariavd commented 5 years ago

Firstly, a great idea to port the big number library for microcontrollers!

I uploaded the examples program sine.pde on an Arduino Uno clone through Arduino IDE 1.8.5 but I get weird locks of the Serial output. The serial console consistently produces only:

sin(0) = 0.00000000000000000000000000000000000000000000000000
sin(1) = 0.01745240643728351281941897851631619247225272030713
sin(2) = 0.03489949670250097164599518162533293735482457604328
sin(3) = 

I thought that I could print the time when each iteration begins, and oddly enough, the loop reached 30 degrees before locking again.

Printing the iteration index of the sine routine shows that it always would reach precision = 19 and then just dies:

1.00000000000000000000000000000000000000000000000000
0.49999999999999999999999999999999999999999999999981
Time: 21665 ms  sin(31) = 30
29.00000000000000000000000000000000000000000000000000
28.00000000000000000000000000000000000000000000000000
27.00000000000000000000000000000000000000000000000000
26.00000000000000000000000000000000000000000000000000
25.00000000000000000000000000000000000000000000000000
24.00000000000000000000000000000000000000000000000000
23.00000000000000000000000000000000000000000000000000
22.00000000000000000000000000000000000000000000000000
21.00000000000000000000000000000000000000000000000000
20.00000000000000000000000000000000000000000000000000
19.00000000000000000000000000000000000000000000000000

Do you have any suggestion what could be causing this behavior? I have access to an oscilloscope but I am not experienced with it. Can it be useful for debugging? Thanks.

nickgammon commented 5 years ago

The documentation for the library here mentions:

Each digit requires a byte of memory, plus some overhead (eg. where the decimal point is) - large numbers will soon gobble up the available memory.

The later versions of the IDE must have slightly more overhead than when I tested on an earlier version. If you change the number of decimal places like this:

  BigNumber::begin (45);  

Then it works up to sin(90).

As a general rule with this library, if it locks up try reducing the precision. We are using a processor with only 2 k of RAM.

mariavd commented 5 years ago

Indeed, thank you for your suggestion. Even reducing the precision to 49 decimal digits sorts out the problem. Also, I tested compiling with -O2 instead of -Os and the additional optimisation made it possible to run the code with 60-digit precision. It's not that I truly need that many digits for my own project but I just wanted to find out what is going on.

For the sake of book keeping (50 digits): -Os: Sketch uses 10248 bytes (31%) of program storage space. Maximum is 32256 bytes. Global variables use 380 bytes (18%) of dynamic memory, leaving 1668 bytes for local variables. Maximum is 2048 bytes.

-O2: Sketch uses 12036 bytes (37%) of program storage space. Maximum is 32256 bytes. Global variables use 380 bytes (18%) of dynamic memory, leaving 1668 bytes for local variables. Maximum is 2048 bytes.

I am now closing the issue.