Skip to main content
Arduino

Return to Answer

Commonmark migration
Source Link

The float type has slightly over 7 digits of precision. See https://en.wikipedia.org/wiki/Floating_point.

I want x to be:

 x = 31.5468112;

Bad luck. That's 9 digits of precision. You can get around 7 digits by converting it appropriately, eg. as Majenko said:

 float x = 3154681.124 / 100000; //x = 31.54;
 Serial.println(x, 7);

However that printed:

31.5468101

It got 7 digits right, as advertised.


You can use the BigNumber Library that I wrote. Available from GitHub.

Using that, you can get all the precision you want, within reason:

#include "BigNumber.h"
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 BigNumber::begin (7); // 7 digits after the decimal place
 BigNumber x ("3154681.124");
 BigNumber y = x / BigNumber ("100000");
 
 Serial.println(y);
 } // end of setup
void loop ()
 {
 } // end of loop

Output:

31.5468112

The float type has slightly over 7 digits of precision. See https://en.wikipedia.org/wiki/Floating_point.

I want x to be:

 x = 31.5468112;

Bad luck. That's 9 digits of precision. You can get around 7 digits by converting it appropriately, eg. as Majenko said:

 float x = 3154681.124 / 100000; //x = 31.54;
 Serial.println(x, 7);

However that printed:

31.5468101

It got 7 digits right, as advertised.


You can use the BigNumber Library that I wrote. Available from GitHub.

Using that, you can get all the precision you want, within reason:

#include "BigNumber.h"
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 BigNumber::begin (7); // 7 digits after the decimal place
 BigNumber x ("3154681.124");
 BigNumber y = x / BigNumber ("100000");
 
 Serial.println(y);
 } // end of setup
void loop ()
 {
 } // end of loop

Output:

31.5468112

The float type has slightly over 7 digits of precision. See https://en.wikipedia.org/wiki/Floating_point.

I want x to be:

 x = 31.5468112;

Bad luck. That's 9 digits of precision. You can get around 7 digits by converting it appropriately, eg. as Majenko said:

 float x = 3154681.124 / 100000; //x = 31.54;
 Serial.println(x, 7);

However that printed:

31.5468101

It got 7 digits right, as advertised.


You can use the BigNumber Library that I wrote. Available from GitHub.

Using that, you can get all the precision you want, within reason:

#include "BigNumber.h"
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 BigNumber::begin (7); // 7 digits after the decimal place
 BigNumber x ("3154681.124");
 BigNumber y = x / BigNumber ("100000");
 
 Serial.println(y);
 } // end of setup
void loop ()
 {
 } // end of loop

Output:

31.5468112
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 69
  • 125

The float type has slightly over 7 digits of precision. See https://en.wikipedia.org/wiki/Floating_point.

I want x to be:

 x = 31.5468112;

Bad luck. That's 9 digits of precision. You can get around 7 digits by converting it appropriately, eg. as Majenko said:

 float x = 3154681.124 / 100000; //x = 31.54;
 Serial.println(x, 7);

However that printed:

31.5468101

It got 7 digits right, as advertised.


You can use the BigNumber Library that I wrote. Available from GitHub.

Using that, you can get all the precision you want, within reason:

#include "BigNumber.h"
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 BigNumber::begin (7); // 7 digits after the decimal place
 BigNumber x ("3154681.124");
 BigNumber y = x / BigNumber ("100000");
 
 Serial.println(y);
 } // end of setup
void loop ()
 {
 } // end of loop

Output:

31.5468112
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /