Problems with BigNumber library

Hi,

Is there a easy way to cast BigNumber to double or float? or the otherway around?

Thank
Per

Hi,

Is there a easy way to cast BigNumber to double or float? or the otherway around?

Thank
Per

Have you tried something like this?

#include <BigNumber.h>
float casted;
BigNumber big;


void setup(){
BigNumber::begin();

}

void loop(){

casted = (float) big;

}

I am trying to use BigNumber.h for big number variables and for doing calculations with them. so far i am getting 0 as result. Am i doing something wrong?

#include <BigNumber.h>

#define MicrosMins 60000000    // micros to minutes multiplier

BigNumber currentPersent;      // decimal must have 15+ resolution since will be like 0.0000000000000000002% on each look
BigNumber totalPersent;        // decimal must have 15+ resolution
BigNumber lastPersent;         // decimal must have 15+ resolution
BigNumber TimeMicros;          // converting millis to micros will give a BIG number
unsigned long Time;            // will store minuetes and will be variable acording to sensor input
unsigned long currentMathMicros;
unsigned long rotationMathMicros;
unsigned long previousMathMicros;



void setup()
{
  BigNumber::begin (50);
  Serial.begin(9600);
  Time = 0;
  rotationMathMicros = 0;
  currentPersent = 0;
  previousMathMicros = 0;
  totalPersent = 0;
}



void loop()
{
  currentMathMicros = micros ();

  TimeMicros = Time * MicrosMins;

  rotationMathMicros = currentMathMicros - previousMathMicros;

  currentPersent = (100 * rotationMathMicros) / TimeMicros;

  totalPersent = totalPersent + currentPersent;

  previousMathMicros = currentMathMicros;

  Serial.print("BIG NUM Persent Done: "); Serial.print (totalPersent); Serial.println(" %");
  Serial.print("BUG NUM Time Micros: "); Serial.println (TimeMicros);
}

DonPedro34:
Is there a easy way to cast BigNumber to double or float? or the otherway around?

You can convert a BigNumber to a double or float by using the normal libraries (on the Uno double and float are the same anyway). For example:

#include <BigNumber.h>
void setup()
{
  BigNumber::begin (50);
  Serial.begin(115200);
  Serial.println ();
  
  BigNumber foo ("123.456789");
  char * s = foo.toString ();
  float bar = atof (s);
  free (s);
  Serial.print ("foo = ");
  Serial.println (foo);
  Serial.print ("bar = ");
  Serial.println (bar, 8);
}

void loop()
{
}

Output:

foo = 123.456789
bar = 123.45678710

You can convert from a float to a BigNumber in a similar way by turning the float into a string, and then using that in a BigNumber contructor (like above). Note that floats don't have more than about 7 digits of precision.

Constantine86:
Am i doing something wrong?

Yes, you are converting to a BigNumber too late. See, for example: Integer and arithmetic overflow.

Also I don't see where Time is incremented:

  TimeMicros = Time * MicrosMins;

Yes, you are converting to a BigNumber too late. See, for example: Integer and arithmetic overflow.

Also I don't see where Time is incremented:

Thank you for your fast response, as i am a newbe this casting trick looks very useful, does it work for doing maths with different type of variables? (int*float-long etc)

Time is constantly changing over each program loop based on sensor input and by doing another equation that outputs data to Time variable.

By to late you mean i should use BigNumber only at void setup? I thought i can use BigNumber as variables to store data and do some basic maths (+-*/) with them on void loop.

No I don't mean that.

#define MicrosMins 60000000    // micros to minutes multiplier
...
unsigned long Time;

So, "Time * MicrosMins" will be unsigned long. Which will probably overflow. Then you try to store that result into a BigNumber.

More like:

  TimeMicros = BigNumber (Time) * BigNumber (MicrosMins);

Time is constantly changing over each program loop ...

Not in the code you posted, it isn't.

i added the Time variable equation and their variables.

#include <BigNumber.h>

#define MicrosMins 60000000    // micros to minutes multiplier
#define type 200

BigNumber currentPersent;      // decimal must have 15+ resolution since will be like 0.00...002% on each loop
BigNumber totalPersent;        // decimal must have 15+ resolution
BigNumber lastPersent;         // decimal must have 15+ resolution
unsigned long TimeMicros;          // converting millis to micros will give a BIG number
unsigned long Time;            // will store minuetes and will be variable acording to sensor input
unsigned long currentMathMicros;
unsigned long rotationMathMicros;
unsigned long previousMathMicros;
unsigned int P;                    //user multiplier input multiplier from 1 to 100
float SensorInput;                 //sensor input
float castedPersent;


void setup()
{
  BigNumber::begin (50);
  Serial.begin(9600);
  Time = 0;
  rotationMathMicros = 0;
  currentPersent = 0;
  previousMathMicros = 0;
  totalPersent = 0;
}



void loop()
{

  Time = ((Type - (Type * (castedPersent / 100))) / SensorInput) * P;  
  
  currentMathMicros = micros ();

  TimeMicros = BigNumber (Time) * BigNumber (MicrosMins);

  rotationMathMicros = currentMathMicros - previousMathMicros;

  currentPersent = (100 * rotationMathMicros) / TimeMicros;

  totalPersent = totalPersent + currentPersent;

  previousMathMicros = currentMathMicros;
  
  char * s = totalPersent.toString ();
  float castedPersent = atof (s);
  free (s);


  Serial.print("BIG NUM Persent Done: "); Serial.print (totalPersent); Serial.println(" %");
  Serial.print("BUG NUM Time Micros: "); Serial.println (TimeMicros);
}

TimeMicros outputs 4.294.781.440 while it should output 360.000.000

Can you give an example of the right BigNumber syntax for doing some basic maths over on void loop?
BigNumber::begin(50); 50 gives the digit resolution, right? Gammon Forum : Electronics : Microprocessors : Arbitrary precision (big number) library port for Arduino i am trying to see if i can find more info on this post

  Time = ((Type - (Type * (castedPersent / 100))) / SensorInput) * P;

Error compiling:

/tmp/arduino_modified_sketch_725756/sketch_aug24a.ino: In function 'void loop()':
sketch_aug24a:37: error: 'Type' was not declared in this scope
   Time = ((Type - (Type * (castedPersent / 100))) / SensorInput) * P;

This is a bit time-wasting, posting code that doesn't compile and asking why it outputs certain results.

This thread is really nothing to do with the announcement thread about the library. I have split it off into a new thread.