0

How can I count how long the floating point is?

e.g.

count(10.123); //result: 3
count(10); //result: 0
count(10.3771) //result: 4

I know how to count it when transforming to a string, but that isn't very efficient, is it?

  • 8
    You need to convert to a string. Counting digits is meaningless on a float. It only means something with its string representation (and only for a specific base) – puhlen Sep 19 '16 at 19:33
  • 3
    Transforming it to a String is definitely the easiest way, and I'll wager dollars to donuts that you're not creating an important, performance critical application worth hundreds of thousands of dollars. – Kayaman Sep 19 '16 at 19:34
  • 3
    How long is decimal representation of one third (`1/3`)? – PM 77-1 Sep 19 '16 at 19:35
  • Converting to a String is the most efficient way of doing this. – Dawood ibn Kareem Sep 19 '16 at 19:36
  • 1
    What do you expect `count(10.000)` to return? – Solomon Slow Sep 19 '16 at 19:37
  • http://stackoverflow.com/questions/29080804/java-how-to-count-the-number-of-decimals-contained-in-a-double-value-that-repr – Tunaki Sep 19 '16 at 19:40
  • How many digits are after the decimal point depends on how much precision you specify when converting to a string (e.g. in `printf`). – Klitos Kyriacou Sep 19 '16 at 19:56

1 Answers1

1

A double is always a specific length. It will only show the numbers need to obtain the accuracy.

ie. 10.3771 is the equivilant to 00000010.37710000 (not the exact number of 0's that are really there, I'm just trying to explain the concept).

In reality even this is inaccurate as a double is a 64 bit binary number

Converting it to String is your best bet and not an inefficient method.

yitzih
  • 3,018
  • 3
  • 27
  • 44
  • 5
    Well, actually 10.3771 is stored as 10.3771000000000004348521542851813137531280517578125, which is the nearest double precision number to 10.3771. – Dawood ibn Kareem Sep 19 '16 at 19:39
  • @DavidWallace, really? That number has more than fifty digits, but a double can not even represent twenty digits of precision. Also, those are decimal digits, but a java double always is an IEEE _binary_ floating point number. – Solomon Slow Sep 19 '16 at 19:43
  • 4
    @jameslarge Yes, really. Given any number, you can write it in binary or in decimal; so there's really no such thing as a "decimal number" or a "binary number". A java `double` has 64 bits, which is just the right amount to store this particular number. – Dawood ibn Kareem Sep 19 '16 at 19:46
  • @DavidWallace Point is that it doesn't make sense to talk about "how it's actually stored" and continue to use base 10. Providing the base 2 representation would be less deceptive. – Zong Sep 19 '16 at 19:49
  • 6
    No, it's really good for someone to understand "how it's actually stored" so that they use `float`, `double` and `BigDecimal` appropriately. If I wrote that number with a series of 0s and 1s, it wouldn't be clear that it's _not_ exactly 10.3771. – Dawood ibn Kareem Sep 19 '16 at 19:49
  • 1
    I agree that understanding floating point numbers is great. My disagreement lies in that by stating a large decimal number as "the true value", it ignores the fact that the underlying representation is binary (which *is* one of the reasons why floats aren't "exact") and also implies a high degree of precision (which doesn't exist). 1s and 0s may be confusing, but too bad, because it's the truth. – Zong Sep 19 '16 at 21:41
  • @ZongZhengLi IEEE 754 defines a mapping from the finite floating point numbers into the real numbers. The result always has an exact representation as a terminating decimal fraction. – Patricia Shanahan Sep 20 '16 at 03:42
  • @PatriciaShanahan You're right, but it doesn't support the argument. Sure, floats are a subset of reals, but please explain why 0.1f is not equal to the real number 0.1. It has something to do with binary, doesn't it? Also, it's not surprising that all floats have terminating decimal fractions, since 2 is a prime factor of 10. If the binary fractions terminate, then so must the same values in decimal. Still doesn't mean the decimal fractions are short or a good representation. – Zong Sep 20 '16 at 07:52
  • @ZongZhengLi There is a finite set of real numbers that can be exactly represented in an finite sized numeric format, including double. 0.1 is not one of them. – Patricia Shanahan Sep 20 '16 at 12:48