Int and Float and Print Formating
Int and Float and Print Formating
Int and Float and Print Formating
In the C family of languages, if you divide an integer by another integer, the compiler interprets the
division operator (/) as an integer division, i.e. you get the integer part of the quotient. Thus,
If either operand is a floating point number, then the other one is implicitly converted to floating
point, and you get a floating point division. Thus
Edgar Bonet
36.5k33 gold badges2828 silver badges5858 bronze badges
shouldn't one of your variables in the second code line be a float? All ints divided will still be an
int – dinotom Apr 22 '16 at 11:54
3.0, as any constant having a decimal point, is a floating point number. As opposed to 3, which is an
int. – Edgar Bonet Apr 22 '16 at 13:20
add a comment
3
In languages such as C or C++ there is a distinction between integer types and floating-point types.
You see, computers work using bits. A bit is either 0 or 1. A bit obviously lack in both range and
precision, so we combine multiple bits in order to improve.
Integral Types
A byte is a group of 8 bits. In C, the type used is either
Floating-point Types
All of the above types are integrals, so they do not support any fractional value. Therefore C
defines two different floating-point types: float and double. The former is a 32-bit value and the
latter is a 64-bit value. The way it works is that the bits are split in 3:
The compiler is smart, but it doesn't hurt to help it. There is only a very small precision loss in
converting this to a float multiplication:
(a + b + c) * (1.0f / 3.0f);
The compiler will pre-calculate (1.0f / 3.0f), so you get a much faster floating-point
multiplication (in addition to the int to float cast).
Note: Arduino Uno defaults all floating point literals to 32-bit, and double is the same as float.
Therefore, the f suffix is strictly not needed. It is still good practice to add these for compatibility
with other architectures.
Share
Improve this answer
Follow
edited Apr 26 '16 at 20:27
answered Apr 22 '16 at 19:23
Pål-Kristian Engstad
13122 bronze badges
Your answer would be correct on many CPU architectures. However, the question is tagged “arduino-uno”,
which implies an AVR architecture, and there are three specific details in your answer which
are incorrect in this specific architecture: 1) chars are signed. 2) ints are 16 bits, not 32. 3) doubles are 32
bits, just like floats. Then, obviously, the f suffix has no effect at all
void setup() {
lcd.begin(20, 4); // set up the LCD's number of columns and rows:
}
void loop() {
// ---------------------------------------------------------------------
// Display values in different formats. Some work, some don't
// In the last row, show how dtostrf and dtostre work
//
void update_LCD(LiquidCrystal lcd, int v1, int v2, float v3, float v4) {
// -- Row 2: display integers with two digits, and attempt to use the
// standard sprintf notation to convert floats to strings.
// This DOES NOT WORK because the Arduino stdio library does not
// include a full implementation of sprintf.
lcd.setCursor(0,2);
sprintf(display_string,"%2d %2d %4.2f %7.3e",v1,v2,v3,v4);
lcd.print( display_string );
1 float example = 3 / 5 * 2;
2 Serial.print("example = ");
3 Serial.println(example);
// Prints: example = 0.00
4
You might assume that the serial monitor prints “1.20”. Afterall, that is the answer
you get when you plug it into your calculator. However, the Arduino math comes
back with “0.00”. Why does that happen?
Similar to the example above, all of the constants on the right of the equal sign
are integers. So each intermediate math operations get stored an integer data
type.
A straightforward way to fix this problem is to tell the compiler that one of the
constants is a floating point variable. Just adding “.0” promotes the operation to
floating point math. Later I’ll talk about preference order which makes a
difference on where you add the .0. If you want to be safe, add it to all integer
constants.
1
float example1 = 1.23456789;
2 float example2 = 123456.789;
3 Serial.print("example1 = ");
4 Serial.println(example1,9);
5 Serial.print("example2 = ");
6 Serial.println(example2,4);
7
// Prints:
8 // example1 = 1.234567880
9 // example2 = 123456.7890
10