How to get more decimal places after division of two floats

Hello,
In my project I came across a point where I've to divide two float values :

  r_ratio = r_ac_float / r_dc_float;
  ir_ratio = ir_ac_float / ir_dc_float;
  R = r_ratio / ir_ratio;

These r/ir_ac/dc_float these all 4 values is upto 2 decimal places and thats acceptable.

But, I want r_ratio and ir_ratio to be having more decimal places than just 2. Also I want R to be more decimal places. because it affects my next final answer :slight_smile:

Edit : I dont have to print this R value, I think I can use that
Serial.println(R,6);
or something like that,I need to calculate it and the nuse it in further calculations

Thanks in advance
Aniket.

You seem to have answered your own question.

@anon73444976 please elaborate.

The only reason you see 2 decimal places is because that is the default for Serial.print.

what i need to change in the above code so that i can get more decimal places in R

I can tell you as a mathematician and coder for over 40 years, on these 8 bit computers that if you desire higher precision, DON'T USE FLOATS.

Arduino has 64-bit type long long and type unsigned long long variables with 19 places precision. Arduino floats will give you six place mantissa and an exponent and the dirt in sand IEEE floating point technique... look that up for how and why, I just avoid floats.

But Nick Gammon shows Arbitrary precision (big number) library port for Arduino.

And a tip: decimal points are for those stuck on standard units.
Engineers work in milliamps and millivolts rather than always full amps and volts.

as I've mentioned. I dont need to print it, I need to store it and use it furteher expression. How to do that

appriciated your reply, so how I can get more decimals?

float x = 10 / 11;
Serial.println(x,5);

this i know
but
what will happen for:

float x = 10 / 11;
Serial.println(x,5);
float y = x/3;
Serial.println (y,6);

the dixision of x for y , it will use upto 2 decimal places of x only not 6. I agree that you can display that result (y) into 6 digits, but actually the division is done the output will be :
0.909090
0.300000

because 0.90/3 = 0.30 it doesn't take all digits of x for division :slight_smile:

Thats why I need more decimal places in the variable, not only for showing

If I want 3 places precision on volts my working unit would be microvolts, giving me 6 places where I can lose 3 and still have volts.millivolts to display.

I'm not sure I understand that sentence.

try this

float myFrickingVariable = 1.987654f;

void loop()
{
Serial.println( myFrickingVariable, 6 );
}

Does it print more than 2 places? If so how did it print more than 2 decimal places if there was not more stored for it to print?

Why not put a float value into the float variable? Why put an int value into a float variable?

float x = 10.0f / 11.0f;`

So true …

You have realise too , that a lot of decimal places doesn’t mean accuracy to that level .
If you are dividing 10/11 then the answer to 6 decimal places is not accurate as the numbers in the sum are to zero decimal places

Ie it’s not 10.00001/11.00001. Or even 10/11.3

Put some numbers in a calculator and see the effect of precision in that 10 and 11and see the effect in the answer .

What kind of variables are you working with? Where do the numbers come from? Are they measurements? To what level of precision are your measurements?

Floats are fine, as long as you don't need more than 6 or 7 correct digits. And, if your numbers represent measurements, probably everything after 6 digits is garbage anyway.

If you really need more digits, then you might want to have the Arduino do the division the old-fashioned way, using this method: https://www.youtube.com/watch?v=MAYRaGY6vm8&t=177s

When I was fixing/writing accounting code in the 80's, every cent mattered.
My electric billing code got checked out by a watchdog agency (Herb Dennenberg's) over being 1 cent different from the electric company's --- turns out they were wrong and the whole issue of putting my client out of business went POOF and PECO likely never changed a thing in their method.

The fundamental problem with IEEE floats.

Perhaps that's why I use the word precision, which is NOT about accuracy.

I’m trying to be helpful here … The answer is there is no sensible reason to want that number of decimal places !!!

This is what I get using that code:

0.00000
0.000000

Changing to floating point division:

  float x = 10.0 / 11.0;
  Serial.println(x, 5);
  float y = x / 3;
  Serial.println (y, 6);

0.90909
0.303030

I do not see the problem you are claiming to have.

Note that float has 6 to 7 digits precision - not decimal places. If you have a number in the millions then the precision is taken up by the whole number part and no decimal digits are significiant.

One of the reasons IBM tended to use binary coded decimal on their computers, accountants get very nervous when told there is no way to exactly represent 10 cents are $0.10 when using binary.