Question about division with float variables

Hello all,
I'm getting results which I am not understanding using the same division with fixed values vs global variables - which are float variables.

Below - I have function1 and function2
Theoretically - these should return the same division results
The first function does not use global variables
The second function does

// Global Variables
float number = 99999999;
float divisor = 10000000;

void function1() { 
    float result = 99999999/10000000;
    byte currentnum = (int)result;
    Serial.println(currentnum);
}

With the function above - the value for "result" is 9.9999999
This is what it should be
The correct integer derived from that is 9 which is correct

// Global Variables
float number = 99999999;
float divisor = 10000000;

void function2(){
    float result = number/divisor;
    byte currentnum = (int)result;
    Serial.println(currentnum);
}

This function is different in that it is drawing off global variables declared at the onset of the code.

But the values are exactly the same.

In this case it calculates "result" with the value of 10.00
Which is incorrect.

What am I missing??

Sincere thanks!

Actually, no, for several reasons. The function has the following line, but the right hand side is an integer divide, the result of which is 9. However, the compiler has several options for how to deal with the result. Compiler optimization will ignore the divide and simply store some value in "result" (*);

    float result = 99999999/10000000;

Second, float variables are 32 bits and have only 6-7 digits of precision total. 9.9999999 cannot be exactly represented by a float value.

(*) Just tested, and on the Arduino Uno R3, the stored result is 9.0, as expected. The following code prints 100000000.00000000 for the same reason, because 99999999 cannot be stored exactly in a float, so it is rounded up.

float y=99999999;
Serial.println(y,8);

AH!
Thank you!

I suspect this would be the case whether I used local variables or global variables.

I'm guess then that I should be using a double data type - rather than the float. I should have checked to verify the range of values it can contain.

So if I want to use variables - for these types of number - what would you suggest as the correct syntax?

Sincere thanks!

double pi = 3.14159265358979;

Unfortunately AVR based Arduinos like the Uno R3, Mega, etc. do not support type double. They treat double exactly like float.

ah!
I see!!
Thank you very much!!! :-]

What are you trying to do, exactly? What do these numbers represent? How much precision do you really need?

On the answers to these questions depends what format is best for storing the numbers.

Hi odometer,
I've got it working now.

I was playing with an 8-digit seven-segment display - seeing what numbers I could get it to display up to its maximum displayable value.

I changed my approach and made the process much simpler
It occurred to me to simply take whatever number it would display with 8 digits and and convert that number to a string.

Then its a simple matter of iterating through each numeric character in that string and sending them in their order to the display.

A much easier approach!
Thanks

You have the intention of assigning a floating point number (integer part + fractional part) to the number variable. If so, why are typing 99999999 instead of 99999999.0? If you want to type really 99999999, then you should make the following declaration:

unsigned long number = 99999999;

To show the number on an 8-digit 7-segment display unit, you may use the modulas(%) and division(/) operators to extract the digits from the given "int type number" and then consult the digit-vs-cc table to get the cc-codes.

Yes thanks!
The approach I have now seems very simple and easy.
But I greatly appreciate your comment!

1 Like

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