I am on OSX Mavericks, trying to learn the printf command in shell and in awk. I was playing around with the different format specifiers and field precision values.
I got a very unexpected result, and I really don't understand why.
This is my code:
#!/bin/bash
a="12345.123456789012345678901"
printf "(%40.30s)\n" $a
printf "(%40.30f)\n" $a
printf "(%40.30e)\n" $a
I had expected to see something like this:
( 12345.123456789012345678901)
( 12345.123456789012345678901000000000)
( 1.234512345678901234567890100000e+04)
But instead, this is my actual result:
( 12345.123456789012345678901)
( 12345.123456789011470391415059566498)
( 1.234512345678901147039141505957e+04)
The first line worked as expected, but the numbers displayed in the 2nd and 3rd (with %f and %e respectively) were not. Notice that the first 11 decimal places were correctly displayed, but the rest were not.
I double-checked using the printf in awk and got the same modified number. (I don't know this for a fact, but based on my testing, the awk printf and the shell printf behave differently, and that is why I tested it in awk too.)
I don't think it has anything to do with rounding. Have I inadvertently triggered some octal or hexadecimal system?
Thanks.