Floating point precision

If we multiply k numbers (say k=3). First stored in double t1 = 5/4 second in double t2 = 4/3 and third in double t3 = 3/2.
Now t4=1.0 *t1 * t2 * t3 (i.e in this case 5/2 = 2.5)
Will this always be same as val = 5/2 despite of the fact that i have solved each of them separately i.e will it always have the actual value or there may be some loss in precision while multiplying.
I am taking product in such a way that corresponding terms always cancels.
So will t4 be same as 5/2(in this case) always.

I have tried some random cases like 22/7 * 7/2 and some more. It always gives the correct answer but idk the reason. Can someone please help !!

This question came out of a question asked by my friend related to floor addition. If required i can share that problem statement too.

I assure you it has nothing to do with any of the live contest. :slight_smile:
Thanks in advance

As per my experience, it will always have the actual value. Please share the problem statement, so that I can also have a look to it…

1 Like

use
(double)5/(double)2

right now your logic is (int)5/(int)2 that will give you 2 not 2.5 and it will store in double as 2.0

1 Like

Problem statement was like that.
You are given n numbers for each number i you have to find floor(a[i]/a[j]) for 1<=j<=n 1<=i<=n and then add those n2 terms.
Complexity should be less than n^2
Although my logic proved to be wrong which was using this thing.
But still Thank you for your reply :slight_smile:

Yeah actually i was using 1.0*5/2 which was stored in double hence it was not the int but double value.

@vijju123 @meooow I would be glad if you too have a look at this question :slight_smile: !!