0

In Eiffel, after doing some operations on a value of type DOUBLE, I get the result 1.9999999999999998, while the expected result is supposed to be 2.

I know that this is caused due to inaccuracies in floating point arithmetic. I need only two digits of precision (while using the DOUBLE type), and hence, if the number could be rounded off to two digits, I think the result would be 2 as expected.

I have seen this question, but it only talks about displaying two digits, while I would like to know if there is a way to round off and store the value with two digits of precision.

Thank you.

Community
  • 1
  • 1
GoodDeeds
  • 7,956
  • 5
  • 34
  • 61

1 Answers1

1

dcm and gobo libraries (both included in standard EiffelStudio distribution under contrib) - provide classes DECIMAL and MA_DECIMAL that can be setup to store and manipulate numbers with specified precision. However, precision here is a total number of significant digits, not a number of digits after a decimal dot. If you need a specified number of digits after a decimal dot, use properly scaled integer types instead.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Thank you. Could you tell me how to use the classes? It says "Error: type is based on unknown class", when I tried to inherit from `DECIMAL`. – GoodDeeds Nov 05 '16 at 09:15
  • You need to add the corresponding library to your project (e.g., see the second part of a [tutorial video](https://www.youtube.com/watch?v=rfXKFIPFdw8)), in the list of libraries it is listed as `decimal`. Also, note that `DECIMAL` is supposed to be used instead of `REAL_64`/`DOUBLE`, so it is not to be inherited, but used as a supplier. – Alexander Kogtenkov Nov 05 '16 at 20:28