Timeline for How to find numbers in a textfile that are not divisible by 4096, round them up and write new file?
Current License: CC BY-SA 4.0
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 19 at 4:58 | comment | added | Daniel Wagner |
You can avoid rounding issues using bc (and, as a side benefit, the resulting command is shorter). I've posted an answer showing how.
|
|
| Jun 18 at 3:20 | vote | accept | Banana | ||
| Jun 17 at 10:11 | comment | added | Peter Cordes |
Computers work with big numbers all the time. Answers with arbitrary limitations other than the type width should explain themselves. Fixed that for you. Hauke's answer checking the remainder shouldn't have this problem. (fmod is more expensive to compute than simple div / mul / subtract, because it has to avoid rounding in cases like this.)
|
|
| Jun 17 at 10:09 | history | edited | Peter Cordes | CC BY-SA 4.0 |
Explain the precision limits.
|
| Jun 17 at 9:56 | comment | added | Romeo Ninov | @PeterCordes, correct, just checked. But still this is big number :) | |
| Jun 17 at 9:52 | comment | added | Peter Cordes |
The smallest number it fails for is 2^54 - 4096 (0x3ffffffffff000 = 18014398509477888), which is about 1.8 x 10^16. So there are multiple int64_t values where this fails. That's larger than the OP's examples, but small enough that it should definitely be mentioned as a limitation. (see the limits section of the wiki article). I tested just now with GNU Awk 5.3.1, and indeed it rounds 18014398509477888 up to 18014398509481984
|
|
| Jun 17 at 9:49 | comment | added | Romeo Ninov | @PeterCordes, let say it on this way: the above code will work when the inputs are integer numbers less than 10^308 (which is huuuge number). At the end my answer is based on the OP input which show integer numbers which are not so big :) | |
| Jun 17 at 9:46 | comment | added | Peter Cordes |
In IEEE binary64 math, DBL_MAX + 4095 == DBL_MAX;. (godbolt.org/z/bjznaWPj4). So yeah this is safe from overflow even for DBL_MAX which is already a multiple of a large power of 2. Still possible there could be a round-up error at the exponent value where representable numbers are 2 apart. 2^e - 4096 is representable for that exponent; adding 4095 rounds up to the next power of 2. That's a problem for many exponent values, when representable numbers are between 2 and 4096 apart, inclusive. en.wikipedia.org/wiki/Double-precision_floating-point_format
|
|
| Jun 17 at 9:35 | comment | added | Peter Cordes |
Can this ever incorrectly round up for huge numbers? Awk uses floating-point for all numbers, IIRC. At some exponent, representable doubles will be 2048 apart, so adding 4095 will actually round up to a multiple of 2048, so it will be like adding 4096. But that's fine because the number is either a multiple of 4096 to start with, or 2048 away from that, not 1. Maybe an addition that crosses an exponent boundary? Will it fail for DBL_MAX + 4095 giving +Infinity, or does that round back down to DBL_MAX? (With integer math a common trick is ((x-1)|4095) + 1 to avoid overflow)
|
|
| Jun 17 at 5:17 | history | answered | Romeo Ninov | CC BY-SA 4.0 |