-1

when divide in c# Answer is different and when i divide from calculator answer is different kindly solve it.

c#

double div=(double)100000/30/9;

Answer 370.37037037037038

370.37037037037038*9*30 //100000.0000000000026


in calculator

Answer 370.3703703703704

370.3703703703704*9*30 //100000

i need exact answer like a calculator.

hamza
  • 1
  • 2
  • Try testing by compiling with debug and release. The debug compile simulates floating point arithmetic while release uses floating point unit (FPU) in microprocessor. Often two methods give slightly different results. Some microprocessors have/had bugs in FPU and patches exist for the bugs. The patches also have bugs. And the the manufactures of the micro's fixed the bugs. So all different combinations of issues exist with floating point math. – jdweng Jun 10 '17 at 01:32

1 Answers1

1

The difference is simply one of rounding. That is not a rational number, it has a repeating sequence, and cannot be perfectly expressed in binary code. It cannot even be expressed with a finite number of digits in decimal. The only difference there is that your calculator displays fewer digits than the C# double.

The final '04' is simply your calculator rounding '038' upward.

TomServo
  • 7,248
  • 5
  • 30
  • 47
  • thanks and when i multiple the c# answer so i get this answer what's that 100000.0000000000026 – hamza Jun 10 '17 at 01:39
  • 1
    Yes, again, rounding error. `double` representations of most numbers are not exact. All common floating point representations have this same problem. They trade range for precision to some degree or other. – TomServo Jun 10 '17 at 01:47