Hi,
I would like to test the range of the float type variable. But, the result showed was 'ovf' . Anyone tries it before?
float abc = 3.4028235E+38;
void setup() {
Serial.begin (9600);
}
void loop() {
Serial.println (abc);
delay (1000);
}
So, how to use float type variable to display its maximum range?
Hi, AWOL
May you give me an example of using FLT_MAX ?
What is this actually?
Thank you.
IIRC, the Arduino print methods restricted the values it could print way below the max allowed by the system.
FLT_MAX is a define that indicates the maximum float value. Look around in your system for a file called limits.h.
Hi KeithRB,
I opened the limits.h file by using notepad. I can not find the maximum float value there.
So, your explanation in detail is very much appreciated.
Thank you.
From
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Print.cpp
Print can't print large floats:
if (isnan(number)) return print("nan");
if (isinf(number)) return print("inf");
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
Oh, and to really print the full range of the float use dtostrf().
Not sure why it's necessary to test the range of a float variable, since it is clearly defined by the IEEE 754 spec:
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating-point implementations that made them difficult to use reliably and portably. Many hardware floating-point units use the IEEE 754 standard.
The standard defines:
IEEE 754-2008, published in August 2008, includes nearly all of the or...
The range is ±1.18×10^−38 to ±3.4×10^38.
Regards,
Ray L.
Yes, but the IEEE doesn't include details like Double on the Arduino is actually not Double. (Unless you have a Due or Teensy.)
Thank you for all replies.
I want to test the float range because the Arduino website says so.
http://www.arduino.cc/en/Reference/Float
float
Description
Datatype for floating-point numbers, a number that has a decimal point. Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.
Does it mean the info on Arduino website is unreliable?
May I know how to use dtostrf() ? Any example for it?
Thank you.
did some work to print floats in SCI or ENG format some time ago
please check this thread - Proposed update for the printFloat code of print.cpp - Libraries - Arduino Forum -
be sure to make a copy of your print.h and print.cpp
before using the ones in the zip file of post #10
philip_sim:
I would like to test the range of the float type variable. But, the result showed was 'ovf' . Anyone tries it before?
The float data type has particular bit patterns for overflow, positive and negative infinity, and 'not a number'. Possibly others. That is, it can represent certain things that are not actually numbers at all.
philip_sim:
Thank you for all replies.
I want to test the float range because the Arduino website says so.
http://www.arduino.cc/en/Reference/Float
Does it mean the info on Arduino website is unreliable?
May I know how to use dtostrf() ? Any example for it?
void setup ()
{
Serial.begin (115200);
Serial.println ("Starting ...");
float a = 3.4028235E+38;
float b = -3.4028235E+38;
char buf [60];
dtostrf (a, 40, 4, buf); // number, width, decimal places, buffer
Serial.println (buf);
dtostrf (b, 40, 4, buf); // number, width, decimal places, buffer
Serial.println (buf);
} // end of setup
void loop () { }
Output:
Starting ...
340282350000000000000000000000000000000.0000
-340282350000000000000000000000000000000.0000