double * float, will there data loss?

Hello everyone!

I have double p for example.
p has to be really accurate.

Then i have float s what will usually be something like 0.0001 - 2.0

Now the question is.
Could variable p lose its accuracy if I mutliple it with s or it will be same
if variable s would be double?

Have to safe memory any way possible.
Thanks!
p has to be really accurate.

How accurate?

Have to safe memory any way possible.

Really? You can't afford another 4 bytes?
if you multiply double with float variable the result will be double and you won't loose accuracy.

However it depends on where the result is saved, if it's saved into a float variable then accuracy is lost.
@moschops.
if you have a double and double and then you have float and double and you multiple both pairs by lets say 100000, the difference will great.

p has to be double accuracy.

@codekiddy
Thanks
Last edited on
"Double" accuracy doesn't mean anything. A "double" can only represent a very small subset of numbers. Saying you need "double accuracy" is meaningless. The only accuracy that matters is "accurate enough for this particular case". I can present an infinity of numbers that a double cannot represent, and an infinity of values for which any given double becomes ever more unable to even approximate.

It is certainly possible to take a perfectly accurate double, and multiple it by a perfectly accurate double, and get a result that is inaccurate, so if you're dexpecting that a double multiplied can be permanently labelled "accurate", you've misunderstood how floating point numbers work.
Last edited on
The thing is that i really don't know how doubles and float exactly work.
I have read about them but they seems really complicated to me.

I have noticed that double can make more sense on smaller values.
For example 0.000000001, float will be rounded but double will be okay.
if you multiply double with float variable the result will be double
true

and you won't loose accuracy
Maybe not true - the initial value of the float doesn't become any more precise, it simply gains more bits.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{ 
    float  a = 0.01f;
    double b = 0.01;  
    
    double c = 987654321;
    
    cout << setprecision(20);
    
    cout << " a: " << a << '\n';
    cout << " b: " << b << "\n\n";
    
    cout << " a*c: " << a*c << '\n';
    cout << " b*c: " << b*c << '\n';
    
} 
 a: 0.0099999997764825820923
 b: 0.010000000000000000208

 a*c: 9876542.9892420563847
 b*c: 9876543.2100000008941
The thing is that i really don't know how doubles and float exactly work.
I have read about them but they seems really complicated to me.

They work very much like your scientific calculator. You calculator might have 10 digits of accuracy and a 3 digit exponent. So numbers are represented as M.MMMMMMMMM * 10^EEE where M.MMMMMMMMM is called the mantissa and EEE is called the exponent. The numbers maybe *displayed* without the exponent, but they are always represented that way. In other words, 1,234,567 is actually represented as 1.234567 * 10^6.

In float and double, it's basically the same, only the mantissa and exponents are in binary instead of decimal. So basically each is good for some degree the accuracy (the mantissa) and some range of large and small values (the exponent).

Then i have float s what will usually be something like 0.0001 - 2.0

0.0001 - 2.0 can't be represented exactly using float. So when you multiple a number times this, the result will also be inaccurate. Then only question is how close will it be.

When working with float and double, if you want to see if two numbers are equal, you really need to see if they are close to each other, so rather than saying
if (a == b)
say
if (fabs(a-b) < smallNumber)
Hi,

One other thing, float does not have the same precision as double. float has 6 or 7 significant figures of precision, while double has 15 or 16 significant figures of precision. You can see the effect from Chervil's example.

The precision of float is easily exceeded, so if you are worried about it, prefer to use double. The default type in C and C++ is double, so stick with that, unless some graphics library requires float, or you have billions of them and have some way maintaining accuracy.

As mentioned by others, it's the accuracy (a different concept than precision) which matters. Even though there is more precision in a double, it doesn't mean there is absolute or even constant amounts accuracy in all situations.

Having to deal with some precision value (such as +/- 0.005m), is sometimes a good thing, as it models what happens in real world measurement: Nothing is ever exact, so one is forced to allow for this. On the other hand it is a pain when not related to physical measurement, because one just wants an accurate number.

Some compliers have the ability to use exact decimal types, gcc has various types that can be found by #include "/decimal/decimal.h"

Any way good Luck !!
Topic archived. No new replies allowed.