last day (15 days later) » 

17:38
14
A: Why does AWK print "0xffffffffbb6002e0" as "ffffffffbb600000" using printf?

Stephen KittNumbers in AWK are floating-point numbers by default, and your value exceeds the precision available. 0xffffffffbb6002e0 ends up represented as 0 10000111110 1111111111111111111111111111111101110110110000000000 in IEEE-754 format, which is 0xffffffffbb600000. With GAWK, if it’s built with MPFR an...

Thanks. Is the "0+" really necessary? Can't hex numbers just be written in their normal form? I guess the "0+" means 10^0 which is 1, so the hex number is just multiplied by 1?
In my example, the hex number is part of the program code, but can hex numbers also be input? I read some documentation saying that only decimals are recognized in the input, while hex numbers are interpreted as strings. Can you confirm?
0+ means “zero plus”, i.e. add zero, and is the documented way to force AWK to treat a value as a number. - So, 0+ is only necessary in certain contexts? In your example, it's not needed, and AWK threats the value as a hex number?
GNU/awk also has the strtonum() function, which converts a specific string field or variable in decimal, octal or hex format to a numeric value. The -n option is global, and should be used with caution.
@Paul_Pedant correct. -n is actually deprecated and comes with a warning, see gnu.org/software/gawk/manual/gawk.html#Nondecimal-Data.
@StephenKitt That variables don't contain full values only the integer part. In awk -M 'BEGIN { x=0xffffffffbb6002e0; y=x/1024; printf("%x\n%x\n", x,y*1024); }' awk removes the decimals (what is after the dot). Adding a -vPREC=200 makes awk correctly able to process more decimals. Better now?
@Isaac well that’s clearer, yes, but AWK doesn’t remove digits after the decimal point; it rounds the floating point value to fit in the available precision, that’s all.
@Isaac note that %x only shows the integer part, but that doesn’t tell you anything about whether the argument is a floating-point value or not.
17:38
That's an alternative interpretation, yes. But the numeric result is missing the numbers after the dot. Whichever was the reason to that to happen. @StephenKitt
@Isaac it’s not an alternative interpretation. You’re saying that AWK only stores the integer part, which is wrong.
which is wrong Prove it!!! @StephenKitt
@Isaac awk -M 'BEGIN { x=0xfffffffbb6002e0; y=x/1024; printf("%f\n%f\n%f\n", x,y,y*1024); }'
Why is 1152921503455511264.000000 being converted to 1152921503455511296.000000 (note that the end of the value is not 64 but 96).
@Isaac don’t change the topic. You said that AWK only stores the integer part, the test above shows that y stores a fractional part.
17:38
@Isaac: Because it's rounded to fit into the available floating point precision, as Stephen Kitt said (and contrary to your interpretation), and 1152921503455511264.000000 doesn't fit in double precision, so it rounds to 1152921503455511296.000000.
@StephenKitt I am saying that when the number has more than 53 bits the rest is removed (when it is a fractional number). For integers that doesn't happen with the -M option. But also happens for integers if the -M has not been used.
@user2357112supportsMonica So, a number that requires more than 53 bits gets rounded, right?. What about: awk -M 'BEGIN{ x=2^223; y=x+2; printf("%f\n%f\n",x,y) }', Why are neither x nor y rounded ? The number is using more than 200 bits, is it not? Repeat without the -M and see the difference. Explain it, please !!!
I am not changing topic. You are failing to understand. Your "prove" has two different results, not the same value, so, it's a false prove, y is not storing the full fractional part, that is why you don't get the original value back. @StephenKitt

last day (15 days later) »