int divided, but a wrong outcome

Hello Guys,

I have an Integer which value I'd like to divide by 10.
Why does the outcome (also an Int) does not show me "0,5" in case of 50?
I get a "0.0".
I can imagine that a Float would handle that, but the result is the same.

How do I accomplish that?

50 divided by 10 is 5.

Or have I missed something?

Like, your code.

In C and C++ division involving only integers always truncates the result which
is also an integer. If one or both of the arguments to the division are floats the
division is floating point and the result is a float as you'd expect.

This is a trap for the unwary (ie those who haven't gone to the lengths of reading
a good guide to C/C++ and absorbed its indiosyncrasies)

Why does the outcome (also an Int) does not show me "0,5"

Because "int" is a contraction of "integer"?

I can't see your code. But it's wrong. Or maybe your expectations are wrong.

Steve

slipstick:
I can't see your code. But it's wrong. Or maybe your expectations are wrong.

Steve

Or, just possibly, both. (Inclusive OR)

MarkT:
This is a trap for the unwary (ie those who haven't gone to the lengths of reading
a good guide to C/C++ and absorbed its indiosyncrasies)

"indiosyncrasies"[sic]??? Do you know a language where an integer WILL represent values that are NOT whole numbers?? It's pretty much right there in the int type name. int == INTEGER, i.e. - whole numbers.

MarkT:
This is a trap for the unwary (ie those who haven't gone to the lengths of reading
a good guide to C/C++ and absorbed its indiosyncrasies)

This guy isn't keen on reading around the subject

slipstick:
I can't see your code. But it's wrong. Or maybe your expectations are wrong.

Steve

I guess it has to be my expectations. :slight_smile:
I meant I have an integer, containing the value 5. I would like to divide it by 10, to get 0,5.
(Or 15, to get 1,5 in example)
Instead, I get 0.

If I understand some of the more useful reactions correctly, this can only be done if the result is a Float as well.
That figures...
I can do the math afterwards in the application where I'm intent do use the outcome.

You could use your own fixed-point format.

0.5 is precisely representable in binary fixed-point too.

int result1 = 5 / 10;  // result1 will always be 0, because, integer math, integer result
float result2 = 5 / 10;  // result2 will always be 0.0, because, integer math, float result
int result3 = 5.0 / 10;  // result3 will always be 0, because, float math, but integer result
int result4 = 5 / 10.0;  // result4 will always be 0, because, float math, but integer result
int result5 = 5.0 / 10.0;  // result4 will always be 0, because, float math, but integer result
float result6 = 5.0 / 10; .. result6 will always be 0.5, because, float math, and float result
float result7 = 5 / 10.0; .. result7 will always be 0.5, because, float math, and float result
float result8 = 5.0 / 10.0; .. result8 will always be 0.5, because, float math, and float result

Tx RayLivingston,

I found out that turning an int value to a float, I cannot just tell the program to store "int A" to "float B" and then do some dividing math on that float.
I did, however, find something to save an int value to another float sized value, while doing some math on the way:

int A (5);
float B;

B = (float) A / 10.0

FTMZ:
Tx RayLivingston,

I found out that turning an int value to a float, I cannot just tell the program to store "int A" to "float B" and then do some dividing math on that float.
I did, however, find something to save an int value to another float sized value, while doing some math on the way:

int A (5);

float B;

B = (float) A / 10.0

The following will all give exactly the same result:

B = A / 10.0;
B = A / (float)10;
B = (float) A / 10.0
B = (float) A / 10;

If EITHER operand of a binary arithmetic operator is float, the other will be converted to float before the operation is performed. If BOTH operands are int, the result of the calculation will be int. The "(float)" is called a "typecast", and means "convert the following value to float before performing the operation. So, "(float)A / 10" means get the value of A, convert it to a float, and divide that float by 10. Since (float)A is a float, the divide will be a float divide, and the result of the divide will also be a float.

Remember that floats are not exact values, so if you calculate something whose result you expect to be 5.000000000, it might actually end up as 4.999999
Convert 4.999999 back to an int, by simple assignment, and it will give you 4. Oops.
When converting floats to ints, you should always be careful to round...

Thanks so much, guys!
Float is quite a hard nut to crack.
Hard to imagine a number, existing of a whole bunch of individual numbers, being represented by "just" 32 bits.

No need to imagine it, the IEEE 754 specification goes over the representations for
floats in complete detail, brief table here: IEEE 754 - Wikipedia

Useful page: What Every Computer Scientist Should Know About Floating-Point Arithmetic

Like I said...
Hard to imagine.
No high-level technical interpretation of my lack of knowledge of the subject can change that. :wink:

FTMZ:
I cannot just tell the program to store "int A" to "float B" and then do some dividing math on that float.

Yes, you can do that:

void setup()
{
  Serial.begin(115200);
  
  int A = 5;
  float B;

  B = A;  //  store "int A" to "float B"
  B = B / 10;  // then do some dividing math on that float.
  Serial.println(B);  // Displays 0.50
}

void loop() {}

What you were trying to do was "do some (integer) dividing math on 'int A' and store the (integer) result in 'float B'". There is a difference.

Note: There is also a difference between: "B = 100 * 1000;" and "B = 100000;". In the first case, you are multiplying two 16-bit integers together, causing an integer overflow. The result will be truncated to -31072.00. In the second case the compiler sees that 100000 doesn't fit in a 16-bit 'int' so it treats it as a 32-bit 'long'. The result is 100000.00.

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