Getting a float division from integers

I have this piece of code:

 float test =0;

 test = (4194304/(r+(65536*oflow)));

All the components are integers, however I wish to have the result in the form of a float.

How to achieve this?

Regards

Make the integers non integers, or cast them to be floats.

AWOL: Make the integers non integers

As in defining them as floats? Thats a waste of memory :fearful:

There are other ways but this will do it.

test = (4194304./(r+(65536.*oflow)));

Pete

el_supremo:
There are other ways but this will do it.

test = (4194304./(r+(65536.*oflow)));

Pete

Cool. I’ve also found this thread that seems to shoe a few other methods http://forum.arduino.cc/index.php/topic,28742.0.html

I'm not sure how you've defined all of the variables, but I'll assume that r and oflow are *float*s. Then try:

float test =0;

 test = (4194304.0 / (r + (65536.0 * oflow)));

The use of the decimal point after numeric constants tells the compiler to use the float data type in the expression.

econjack:
I’m not sure how you’ve defined all of the variables, but I’ll assume that r and oflow are floats. Then try:

float test =0;

test = (4194304.0 / (r + (65536.0 * oflow)));




The use of the decimal point after numeric constants tells the compiler to use the *float* data type in the expression.

Thanks. None of those variables are floats, but apparently only one needs to be defined as such to have a float result.

I used test = (((float)4194304/(r+(65536*oflow))));

Now I wish my output is given, rounded up to the next integer. For example 0,60 or 0,99 should show as 1.
How can I achieve this?

floats print default two decimal places

try

Serial.println(myFloat, 0);

BulldogLowell: floats print default two decimal places

try

Serial.println(myFloat, 0);

No. I don't wish to print, I wish to store a variable a a rounded value, so that can later be processed by other parts of the code. The only reason i need float is because the arduino doesn't round up by default.

Then, just round it then.

BulldogLowell: Then, just round it then.

I don't know how to do that, hence the question. How do I round up the result of a division!?

Take a look at your options:

#include <math.h>


void setup() {

  float val = 3.5;
  
  Serial.begin(115200);
  Serial.print("floor = ");
  Serial.println(floorf(val));
  Serial.print("round = ");
  Serial.println(roundf(val));
  Serial.print("ceil = ");
  Serial.println(ceilf(val));
  
}

void loop() {

}

econjack: Take a look at your options:

Brilliant, Thank you so much

casemod:

AWOL: Make the integers non integers

As in defining them as floats? Thats a waste of memory :fearful:

How many bytes to represent 65536? How many bytes to represent 65536.0?

AWOL:

casemod:

AWOL: Make the integers non integers

As in defining them as floats? Thats a waste of memory :fearful:

How many bytes to represent 65536? How many bytes to represent 65536.0?

Care to go straight to the point?

My sketch was 2.5K, enabling a single float makes it 4.2K, everything else being equal. A single float wont use that much memory, but I have an assumption that the compiler is not doing the best optimization

Care to go straight to the point?

Both take 4 bytes so to say floats take more memory is a bit silly.

My sketch was 2.5K, enabling a single float makes it 4.2K,

And what does that mean?

My sketch was 2.5K, enabling a single float makes it 4.2K, everything else being equal.

And does "enabling" two floats make the sketch 5.9K? I think not.

How many bytes to represent 65536? How many bytes to represent 65536.0?

AWOL:

My sketch was 2.5K, enabling a single float makes it 4.2K, everything else being equal.

And does "enabling" two floats make the sketch 5.9K? I think not.

Most certainly not, I do get an increase of about 150bytes. That should be good for the float, so the missing 1.7K are still somewhere. Doing god knows what.

AWOL: How many bytes to represent 65536? How many bytes to represent 65536.0?

This is not the point and certainly not my concern. My concern is why the compiler reserves so much space for a float.

Grumpy_Mike:

My sketch was 2.5K, enabling a single float makes it 4.2K,

And what does that mean?

Sketch size in KB, after a "unsigned long int" is replaced with a "float"

For a float constant or variable, the compiler reserves at most, four bytes.