0

How to get the ceil value of numbers like 1.000000000001 to 2 as it is giving 1 by default(i am using CPP),I came across a problem where I needed to calculate ceil(1000000000,999999999) and it is giving 1,is there any other method to de the same??

tadman
  • 208,517
  • 23
  • 234
  • 262
Pranav
  • 1
  • `1.000000000001` is most likely 1 already, so `ceil(1)` is 1 again, not 2. – Quimby Dec 21 '20 at 14:15
  • Are you using `double` or `float` for your returned value? The latter doesn't have sufficient precision to make the final `1` significant. On my system, `printf("%lg\n", ceil(1.000000000999999999));` shows `2` but `printf("%g\n", ceil(1.000000000999999999f));` shows `1`. – Adrian Mole Dec 21 '20 at 14:16
  • 1
    Welcome to the wild and zany world of floating point math as enshrined in the [IEE 754.0000000000000004 standard](https://en.wikipedia.org/wiki/IEEE_754). – tadman Dec 21 '20 at 14:31
  • either use long double - or try one of these https://en.wikipedia.org/wiki/List_of_C%2B%2B_multiple_precision_arithmetic_libraries – PiotrNycz Dec 21 '20 at 14:35
  • By `ceil(1000000000,999999999)` do you mean `ceil(1000000000/999999999)`? If so, you probably want to do `ceil(1000000000.0/999999999.0)`, otherwise the result of `1000000000/999999999` is 1, and `ceil(1)` is 1. – Eljay Dec 21 '20 at 14:46
  • 1
    @Quimby: `1.000000000001` is not most likely 1. In the format most commonly used for `double`, it is more than 4,500 ULP away from 1. – Eric Postpischil Dec 21 '20 at 21:22
  • double has not enough precision to store numbers like `1000000000.999999999`. And even `long double` may also be not enough – phuclv Dec 22 '20 at 03:59

0 Answers0