0

What is difference between Case 1 and Case 2?

I think the result should be the same: 7.78

Can someone explain to me?

Case 1: a=(x/y).round(4)*100

Case 2: a=(x/y*100).round(2)

Case 1

x=7.0 y=90 a=x/y => 0.07777777777777778 a=a.round(4) => 0.0778 a=a*100 => 7.779999999999999 


Case 2

x=7.0 y=90 a=x/y => 0.07777777777777778 a=a*100 => 7.777777777777778 a=a.round(2) => 7.78 
1

1 Answer 1

1

Floating-point numbers cannot precisely represent all real numbers, and floating-point operations cannot precisely represent true arithmetic operations, this leads to many surprising situations.

I advise to read: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.