Arduino Division

float stepsRemaining_float = 0.0;  

stepsRemaining_float = 30/100;
WEBLOG("stepsRemaining_float = 30/100; = %f",stepsRemaining_float);

stepsRemaining_float = 0.3;
WEBLOG("stepsRemaining_float = 0.3; = %f",stepsRemaining_float);

The 1st print results -> 0 which is not expected.
The 2nd print results -> 0.3 which is expected.

Why doesn't the 1st print result in a 0.3 when the variable is declared as a float?
thanks

stepsRemaining_float = 30/100;

By default this will be done as an integer division. Try this instead

stepsRemaining_float = 30/100.0;

or 30.0 / 100 or 30.0 / 100.0

Faster if you use multiplication

 stepsRemaining_float = 30.0 * 0.01;

Because: both of the arguments to the division operator ARE integers, even though the result is being stored in a float…

To clarify what others have already mentioned and to explain what the compiler* does:

When you write:

float stepsRemaining_float = 30 / 100;

The compiler first evaluates the expression on the right-hand side. It sees 30 / 100 and determines the types of the constants. Since both 30 and 100 are integral values and fit into an int, they are implicitly treated as int.

Next, the compiler performs the division operation. In C++, dividing two integers results in an integer, with any decimal part being truncated.

Since 30 / 100 equals 0.3, but both operands are integers, the result is truncated to 0, which remains an int.

Finally, the compiler processes the assignment. It notices that an int is being assigned to a float, which is allowed. An implicit conversion then takes place, converting 0 (of type int) to 0.0 (of type float).

* side note : as all this can be evaluated at compile time, the compilation process will not generate any code for this and just keep

float stepsRemaining_float = 0;

which you had in the previous line and so will just get rid of the statement altogether.

[EDIT: removed confusing link about constant folding — wikipedia entry below by @alto777 is the right idea]

1 Like

This link

spells things out in language closer to readable by mere humans.

Unless constant folding isn't constant folding? I can't make heads or tails of the cppreference.com link, not in my current condition anyway.

I also don't understand

The previous line here

float stepsRemaining_float = 0.0;  

is a definition of a floating point variable with an initial value of 0. How that changes what is done, or not done, with the following assignment is not clear to me.

a7

I meant in his posted code he had first the definition and initialisation and then the assignment.

what I was saying is that the compiling process will be smart enough not to have the two lines

float stepsRemaining_float = 0.0;  
stepsRemaining_float = 0; // <=== as a result of evaluating the expression at compile time

and will only keep

float stepsRemaining_float = 0.0;  
1 Like

sorry - wrong link, too quick of a search and linked without exactly looking at what I was sharing .

Fold expressions in C++17 are a feature specifically for variadic templates, allowing you to apply an operator across all elements of a parameter pack...

For example, in C++17:

#include <iostream>
template<typename... Args> auto sum(Args... args) {
  return (args + ...);  // Fold expression
}

int main() {
  std::cout << sum(1, 2, 3, 4);  // Outputs: 10
}

This expands to:

(((1 + 2) + 3) + 4)

which is evaluated at runtime (or compile time if all arguments are constexpr).


I'll remove the wrong confusing link above

Thank you all for the response. I didn't know it would treat it as an int.
I'm new to this IDE, not really sure how to debug this or step through the code.
I've been uploading to my esp32 and trying it that way in serial.
Looks like there's still a lot to learn.

It seems to me that what you have to learn is really C++

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.