float vs integer

what is the result of "float"/"integer"? A float or an integer?

What, if the second is true, should I do to obtain a float while the divisor is a variable "int x = 8"?

If I understand the question correctly, you need to add decimal places when you declare or set your float value. Float Reference

brice3010:
what is the result of "float"/"integer"? A float or an integer?

C rule for type promotion is:

If an operation involves two operands, and one of them is of type float, the other one is converted to float.

So an operation of "float/int will actually be handled as "float/float, and of course the result will be float.

Same situation with "float*int" or even "float+int".

1 Like

what is the result of "float"/"integer"? A float or an integer?

It is impossible to answer the question without more details of the circumstances

What type of variable is the answer being put in ?
Are the float and integer constants or variables ?
How are they expressed in the calculation ?

And best of all, why do you think you need to? The 8-bit (standard) Arduinos's have no hardware for floats so floats are slow and big. And remember, just because you want a decimal place doesn't mean you need a float :wink: Float != decimal place.

What, if the second is true, should I do to obtain a float while the divisor is a variable "int x = 8"?

Maybe something like this:

int x = 8;
float y;
float z;

y = 123.456;

z = y/x;

septillion:
And best of all, why do you think you need to? The 8-bit (standard) Arduinos's have no hardware for floats so floats are slow and big. And remember, just because you want a decimal place doesn't mean you need a float :wink: Float != decimal place.

x is an integer used for a loop counter

y and z are float values

in the following calculation I need a float for z (or at least a number with two decimals behind the comma):

z = y / x

What should I do to obtain a number for z with two digits behind the comma?

birddog:
If I understand the question correctly, you need to add decimal places when you declare or set your float value. Float Reference

If I understand correctly I should write:

if: x = int and y and z = float

and if I want z to be a float I need to write:

z = y / (float)x

Correct?

jurs:
C rule for type promotion is:

If an operation involves two operands, and one of them is of type float, the other one is converted to float.

So an operation of "float/int will actually be handled as "float/float, and of course the result will be float.

Same situation with "float*int" or even "float+int".

I know this to be true in C. But is it true for the Arduino compiler?

brice3010:
or at least a number with two decimals behind the comma

Which I told you, are NOT the same :wink:

brice3010:
What should I do to obtain a number for z with two digits behind the comma?

It's actually pretty simple... Multiply everything, or in this case y, by 100. Yesss, really! That point is just a concern when printing it. Think of it as keeping track of cents instead of euro's/dollars. As long as you know of what you keep track you know how much you have. And that's what you call fixed point, the opposite of floating point (hence the name float).

brice3010:
I know this to be true in C. But is it true for the Arduino compiler?

Why would that be different? It IS a C++ compiler!

septillion:
Which I told you, are NOT the same :wink:

..I am learning :wink:

septillion:
It's actually pretty simple... Multiply everything, or in this case y, by 100. Yesss, really! That point is just a concern when printing it. Think of it as keeping track of cents instead of euro's/dollars. As long as you know of what you keep track you know how much you have. And that's what you call fixed point, the opposite of floating point (hence the name float).

Please clarify? If I multiply y by 100 sure I must multiply x by 100? Now what do I gain?

z = y * 100 / x /100 ??

septillion:
Why would that be different? It IS a C++ compiler!

Now I am confused: I divide float by integer, y / x, so z = y / x must be a float??

brice3010:
If I multiply y by 100 sure I must multiply x by 100?

No... If you want to divide a euro into 4 and have an answer in cents to avoid the nasty decimal place, do you divide 100 by 40 then? No, you divide 100 cents aka 1 euro by 4 which gives you 25 cents.

You do the same with fixed point, just multiply by 10 until you don't need a decimal point and use that. That decimal point is just a minor detail when you print it. It's just like writing (you know, with a pen and paper) 100 and later thinking, "mm, i wanted that to be in euro's" and you just put a decimal point 2 places from the right. In fixed point math it's exactly the same :wink:

brice3010:
Now I am confused: I divide float by integer, y / x, so z = y / x must be a float??

Why war you confused? He copied the rule for C, Arduino is C...

septillion:
No... If you want to divide a euro into 4 and have an answer in cents to avoid the nasty decimal place, do you divide 100 by 40 then? No, you divide 100 cents aka 1 euro by 4 which gives you 25 cents.

I need z to have two decimals behind the comma; y is a float by the nature of my program and x is an integer by same reason.

Now how do I get the fastest, most effiently and most accurately two decimals behind the comma in z?

If I follow your reasoning, I end up with z being 100 times larger than what it correctly shoud be. So I should introduce another variable w which will be z divided by 100.

Sure this should be a float(w) if I want the decimals behind the comma?

septillion:
Why would that be different? It IS a C++ compiler!

And in fact it uses the gcc compiler which even when it has a feature that is not C, it often becomes C in the next rev of the standard!

brice3010:
y is a float by the nature of my program

Than that's a fundamental design flaw :wink:

brice3010:
Now how do I get the fastest, most effiently and most accurately two decimals behind the comma in z?

If you skip fast, efficient AND accurate aka just easy for you because you don't need to learn, then you simply divide the two.

If you really care about fast, efficient or accurate you reread my answer and analogy with euros/dollars and cents.

brice3010:
So I should introduce another variable w which will be z divided by 100.

No, you leave it that way! And just remember there is a virtual decimal point two places from the right.

If you want to check it with something, make that something 100 times as big aka have a virtual decimal point as well.

If you want to print it (to serial or whatever) just do what I told you, add a decimal point when printing it.

brice3010:
Sure this should be a float(w) if I want the decimals behind the comma?

Nope, like I state in my first answer, float != decimal point! And it's not why a float is invented :wink: It's invented to have a way of storing very small and very large numbers in a limited storage space BUT with reduces accuracy!

He might need to make x a long so as not to overflow an int.

True, but depends on the range he need. :slight_smile:

septillion:
If you want to print it (to serial or whatever) just do what I told you, add a decimal point when printing it.

Ahhhaa! So indeed I need to print with 2 decimals. I get your point I think: I take the integer which is 100 times too large and print with the decimal point 2 positions earlier? I need to find out about that tomorrow, sorry, I am on GMT+1.

septillion:
True, but depends on the range he need. :slight_smile:

Range will be 3 digits, up to 150, so indeed long needed. Thank you!

Yesssss! That's it! And that's exactly what banks do :wink: They keep track of cents, that way they can use an integer which IS exact as where a float isn't, but just add a decimal point two places to the right when printing :smiley:

And I have code to do that pretty efficient. If only I could find it...

Now it's in the range of 3 to 150? Then you don't need an long. Int can hold up to 32767 and 150 x 100 is just 15000. And if you don't need negative just use a unsigned int and you have a range up to 65535 for the same amount of storage in RAM!

PS, same time zone :wink: