0

I have a problem with big numbers in double variables. Imagine I have 3 numbers like this:

  1. 124.556321466
  2. 120.00
  3. 1.79769313486232E+308

I just want to recognize item 2 or those numbers that don't have any valuable number after floating point. In other words those numbers can be integers and the lengths of numbers are not the same.

Two additional things:

  • I don't want to convert them to string because it is really time consuming for the CPU.
  • I can't convert them to integer because I need all the digits.
honk
  • 9,137
  • 11
  • 75
  • 83
andrew
  • 13
  • 4
  • Just convert the double to integer for "recognizing" it. Then subtract the integer from the double and check if the absolute result is smaller than an epsilon value. – honk May 15 '15 at 21:42
  • The third number is also an integer, but outside the range of most limited range integer types. Do you want to treat it as an integer? – Patricia Shanahan May 15 '15 at 22:47
  • Also, what language? There are different methods I would use in different languages. – Patricia Shanahan May 16 '15 at 07:12
  • sorry in c# with type casting i'll loose many of digits Am i right ? – andrew May 16 '15 at 07:26

1 Answers1

1

There are several Math functions, such as Math.Truncate, that return the same value as their input if, and only if, the input is an integer. The simplest way to check for a double being exactly an integer is to check for equality between the original value and the result of Math.Truncate or similar.

However, you may be surprised by some of the results. 4.503599627370496E15 and all larger finite doubles are integer-valued.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75