nickgammon / bignumber Goto Github PK
View Code? Open in Web Editor NEWBigNumber library for the Arduino
License: MIT License
BigNumber library for the Arduino
License: MIT License
hello nick thank you for your awesome libraries!! i wanna ask some if this library can generate random number in BigNumber? or i think if there is a way to convert random number in int or long to type data BigNumber? Thank you so much!!
when compiling for some, such as STM32, boards, I get errors of undefined 'uint32_t' and 'uint16_t'.
I believe that this can be resolved by adding '#include <stdint.h>' just after then multiple include safeguard, say at line 34.
If you have an alternative suggestion, I'm all ears!
This is using the IDE 2.2.1
@nickgammon Hi, I found the lib "BigNumberMath.h" that is used with "BigNumber.h". Maybe someone knows something about this lib? Where I can find the source of code?
Thanks.
P.S. I came from this solar tracking system.
Hi Nick! I am trying to use your library (BigNumber) but i have a problem.
I'm trying to read numbers from a sd card and to take last value assigning it to a variable .
I stored values as BigNumber with this sketch:
File myfile;
BigNumber pot=A3, input1, input2, d1, d2, valoreatt, valoreacc=0;
void printBignum (BigNumber n){
char * s = n.toString ();
Serial.println (s);
free (s);
} // end of printBignum
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
//verifico l'esistenza di una memory card
if (!SD.begin(10)) {
Serial.println("Card failed");
return;
}
Serial.println("card inizialized");
BigNumber::begin (20); // initialize library
input1 = analogRead(pot);
}
void loop() {
if (valoreacc<1){
input2 = analogRead(pot);
d1 = BigNumber (input2) / BigNumber (1023);
d2 = BigNumber (d1) * BigNumber (d1) * BigNumber (d1);
valoreatt = BigNumber (d2) / BigNumber (125) / BigNumber (1000);
valoreacc = BigNumber (valoreatt) + BigNumber (valoreacc);
Serial.print ( "input: " );
printBignum (input2);
Serial.print ( " valoreatt: " );
printBignum (valoreatt);
Serial.print ( " valoreacc: " );
printBignum (valoreacc);
// memorizzo il danno sulla sd card
myfile = SD.open("valori.csv", FILE_WRITE);
if (myfile) {
myfile.println( valoreacc );
myfile.close();
}
input1=input2;
delay(1500);
}
else {
Serial.print( "stop" );
}
}
An example of numbers stored is this:
0.00000069004655222191
0.00000138009310444382
0.00000207013965666573
0.00000276018620888764
0.00000345023276110955
0.00000414027931333146
0.00000483032586555337
0.00000552037241777528
0.00000621041896999719
0.00000690046552221910
0.00000759051207444101
0.00000828055862666292
0.00000897060517888483
0.00000966065173110674
Then I tried to take last value with this sketch:
File myFile;
BigNumber valore;
void printBignum (BigNumber n){
char * s = n.toString ();
Serial.println (s);
free (s);
} // end of printBignum
void setup(){
Serial.begin(9600);
Serial.println("Initializing SD card...");
pinMode(10, OUTPUT);
digitalWrite(10, 1);
BigNumber::begin (20); // initialize library
if (!SD.begin(10)) {
Serial.println("initialization failed!");
}
char c;
String inString = String();
myFile = SD.open("valori.csv");
if (myFile) {
while (myFile.available()) {
while(c != -1) {
c = myFile.read();
if(!myFile.available()) break;
if (c == '\n') inString = "";
inString += c;
}
}
myFile.close();
}
// -----------questa è la tua ultima riga ----------
Serial.println(inString); //debug
//--------------------------------------------------
valore = inString.toInt();
Serial.print ( " valore: " );
printBignum (valore);
}
void loop(){
}
It finds last value but i don't succeed to assign it to variable "valore" to do then others math operations. It gives me 0.
hello
I'm creating a calculator with a pro micro (ATMega32U4)
I receive char from a keypad.
The new keystroke must be the lightest digit:
Serial.print("key presed ");
Serial.println(tecla);
entrada_num = 10 *entrada_num;
int b = String(tecla).toInt();
entrada_num += b;
Serial.print("new value: ");
Serial.println(entrada_num);
The first 5 presses have a correct result, but...
key preset 1
new value: 1,000
key presed 2
new value: 12,000
key presed 3
new value: 123,000
key presed 4
new value: 1234,000
key presed 5
new value: 12345,000
key preset 6
new value: -7616,000
key presed 7
new value: -10617,000
Thank you!
It could be fun to use BigNumber to calculate Pi. I noticed that the sqrt() function is part of the library. That makes it possible to use the fast Francois Viete formula.
The sketch below calculates Pi with 68 accurate decimal digits with an Arduino Uno in 34 seconds.
// BigNumber example: Calculate Pi using the Francois Viete formula
#include <BigNumber.h>
BigNumber PiViete ()
{
// Francois Viete formula
BigNumber two (2); // used as a constant value of 2
BigNumber s (0); // used in iteration
BigNumber t (1); // used in iteration
for (int i = 0; i < 115; i++) // number of iterations: 10 to 115
{
BigNumber r = s + two; // temporary value
s = r.sqrt ();
t *= s / two;
}
return two / t; // calculate pi
} // end of function pi
void setup()
{
Serial.begin (115200);
Serial.println ();
// Arduino Uno
// -----------
// The Arduino Uno has only 2k bytes of SRAM.
// The maximum length of BigNumber is 74 for an Arduino Uno.
// About 115 iterations in the Francois Viete formula is enough for an Arduino Uno.
// That will take 34 seconds.
//
// Arduino Zero or Arduino MKR
// ---------------------------
// For an Arduino Zero, it is possible to set the length of BigNumber to a higher value.
// For example, the length of BigNumber to 1000, and 1500 iterations.
// That will take 8 hours.
//
BigNumber::begin (74); // Length of the BigNumber. 50 is fast, 74 is slow.
Serial.println ("Please wait patiently (34 seconds with Arduino Uno)");
Serial.println ("pi (as text) = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706");
unsigned long start = millis();
BigNumber pi = PiViete ();
unsigned long finish = millis();
Serial.print ("pi (Viete formula) = ");
Serial.println (pi);
Serial.print ("It took ");
Serial.print ((finish - start) / 1000UL);
Serial.print (" seconds.");
} // end of setup
void loop () { }
Instead of bc_sqrt (square), bc_cbrt (cube)
Hi, for some reason using BigNumber::begin begins heating the backside of the Teensy 4.0. If you are not fast enough and restore your Teensy with the self-rescue 15 second button press, this will eventually kill it.
Verified with a minimal sketch and two Teensy 4.0.
I have no idea how this could be happening, just wanted to let you now.
hello, how to send a big number with serial in Arduino ? I have tried to send with serial.write but the result is false
and thanks
Hi.
This Library is Although MIT license seems to be applied, the GNU GPL license is listed in gpl.txt.
When I use this library, which license should I consider?
@nickgammon
Firstly, thanks for your BigNumber library. I get some issue when i use your library,i do not know if it is because of a stupid mistake or other reason.
This is the code
#include <math.h>
#include "BigNumber.h"
BigNumber latitude="35.95568352";
BigNumber longitude="120.1484011";
void setup (){
Serial.begin(9600);
BigNumber::begin (15);
BigNumber pi ("3.1415926535897");
BigNumber a ("180");
BigNumber latitude_rad = latitudepi/a; // latitude in rad
BigNumber longitude_rad = longitudepi/a; // longitude in rad
Serial.println();
Serial.println();
Serial.println(latitude_rad);
Serial.println(longitude_rad);
}
void loop(){}
I've encountered a strange phenomena for casting negative numbers long-type valuables:
long a = -33224;
Serial.println(a);
Serial.println((BigNumber)a);
This code results in:
-33224
32312
The second result of casted BigNumber seems to be the 2's complement of the original value in 16bit width.
The similar casting for int-type value results the correct value.
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.
Casting float-type variable seems to be ignoring fractional part?
BigNumber a = "12.34";
Serial.println(a);
Serial.println((float)a);
This code results in:
12.34
12.00
Is this a specification of BigNumber?
I get the following errors on compile, though they do go away if I keep running the compile...
/Users/.../Documents/Arduino/libraries/BigNumber/number.c: In function 'bc_str2num':
/Users/.../Documents/Arduino/libraries/BigNumber/number.c:1523:7: warning: assignment discards 'const' qualifier from pointer target type
ptr = str;
^
/Users/.../Documents/Arduino/libraries/BigNumber/number.c:1548:7: warning: assignment discards 'const' qualifier from pointer target type
ptr = str;
^
I am trying to do the Diffie Hellman key exchange here. It require a very very large prime number, so I end up using this library. But then the key must be in char[] type.
So is there any proper way to convert?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.