-2

I am calculating a floating point number by formula: number=1/(n-2.001)
Where n is any integer from 1 to infinite. But it give me different answer in laptop and scientific calculator.

C++ calculation : 0.333444
Calculator answer: 0.3334444815

I have to get all digits in c++. How i get this.

Muhammad
  • 39
  • 5
  • What data type do you use? – 273K Jun 20 '21 at 07:17
  • How do you display the output? Please include all relevant code. – DYZ Jun 20 '21 at 07:17
  • Does [this](https://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout) answer your question? – DYZ Jun 20 '21 at 07:19
  • Does this answer your question? [How do I print a double value with full precision using cout?](https://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout) – Retired Ninja Jun 20 '21 at 07:25
  • Another example, similar to Ninja's but slightly different: https://stackoverflow.com/a/50970282/4641116 – Eljay Jun 20 '21 at 12:20

1 Answers1

1

Decimal equivalent of 1/3 is 0.33333333333333….

An infinite length number would require infinite memory to store, and we typically have 4 or 8 bytes. Therefore, Floating point numbers store only a certain number of significant digits, and the rest are lost.

NOTE : When outputting floating point numbers, cout has a default precision of 6 and it truncates anything after that.

The precision of a floating point number defines how many significant digits it can represent without information loss.

  • Therefore in your case only 6 decimals points are outputted and rest are turncated

  • To change the Precision of floating-point data types in C++ check this

anirudh
  • 1,436
  • 5
  • 18