awk high precision arithmetic

mkc

I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. However, I am losing precision there. Here is a simplified reproduction of the problem:

 $ echo 0.4970436865354813 | awk '{gsub($1, $1*1.1)}; {print}'
   0.546748

Here, I have a 16 digit after decimal precision but awk gives only six. Using printf, I am getting the same result:

$ echo 0.4970436865354813 | awk '{gsub($1, $1*1.1)}; {printf("%.16G\n", $1)}'
0.546748

Any suggestions on to how to get the desired precision?

Stéphane Chazelas
$ echo 0.4970436865354813 | awk -v CONVFMT=%.17g '{gsub($1, $1*1.1)}; {print}'
0.54674805518902947

Or rather here:

$ echo 0.4970436865354813 | awk '{printf "%.17g\n", $1*1.1}'
0.54674805518902947

is probably the best you can achieve. Use bc instead for arbitrary precision.

$ echo '0.4970436865354813 * 1.1' | bc -l
.54674805518902943

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

awk high precision arithmetic

From Dev

Decimal with high precision and scale creating 'Arithmetic Overflow Error'

From Dev

Decimal with high precision and scale creating 'Arithmetic Overflow Error'

From Dev

Arithmetic precision problems with large numbers

From Dev

High precision scheduler daemon

From Dev

Arithmetic operations with awk

From Dev

awk field count arithmetic

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

Invalid Arithmetic Operator Inside Awk

From Dev

perform arithmetic operation using awk

From Dev

Axis label in MATLAB for high precision values

From Dev

Configure Windows time with high (sub seconds) precision

From Dev

Best strategy to compute average with high precision

From Dev

High-precision sleeping / waiting in .net

From Dev

How to compute an expensive high precision sum in python?

From Dev

Need to store high precision decimal values in MongoDB

From Dev

High precision floating point numbers in Swift

From Dev

High numerical precision floats with MySQL and the SQLAlchemy ORM

From Dev

Looping at a constant rate with high precision for signal sampling

From Dev

Why KNN has low accuracy but high precision?

From Dev

High numerical precision floats with MySQL and the SQLAlchemy ORM

From Dev

Looping at a constant rate with high precision for signal sampling

From Dev

High-precision sleeping / waiting in .net

From Dev

How to maintain high decimal precision in VBA Excel

From Dev

BBP-Algorithm in Python - Working with Arbitrary-precision arithmetic

From Dev

Oh where has my precision gone with OpenMesh vector arithmetic?

From Dev

Calculating pi without arbitrary precision and only basic arithmetic

From Dev

Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

Related Related

  1. 1

    awk high precision arithmetic

  2. 2

    Decimal with high precision and scale creating 'Arithmetic Overflow Error'

  3. 3

    Decimal with high precision and scale creating 'Arithmetic Overflow Error'

  4. 4

    Arithmetic precision problems with large numbers

  5. 5

    High precision scheduler daemon

  6. 6

    Arithmetic operations with awk

  7. 7

    awk field count arithmetic

  8. 8

    Arbitrary precision arithmetic with very big factorials

  9. 9

    Arbitrary precision arithmetic with very big factorials

  10. 10

    Invalid Arithmetic Operator Inside Awk

  11. 11

    perform arithmetic operation using awk

  12. 12

    Axis label in MATLAB for high precision values

  13. 13

    Configure Windows time with high (sub seconds) precision

  14. 14

    Best strategy to compute average with high precision

  15. 15

    High-precision sleeping / waiting in .net

  16. 16

    How to compute an expensive high precision sum in python?

  17. 17

    Need to store high precision decimal values in MongoDB

  18. 18

    High precision floating point numbers in Swift

  19. 19

    High numerical precision floats with MySQL and the SQLAlchemy ORM

  20. 20

    Looping at a constant rate with high precision for signal sampling

  21. 21

    Why KNN has low accuracy but high precision?

  22. 22

    High numerical precision floats with MySQL and the SQLAlchemy ORM

  23. 23

    Looping at a constant rate with high precision for signal sampling

  24. 24

    High-precision sleeping / waiting in .net

  25. 25

    How to maintain high decimal precision in VBA Excel

  26. 26

    BBP-Algorithm in Python - Working with Arbitrary-precision arithmetic

  27. 27

    Oh where has my precision gone with OpenMesh vector arithmetic?

  28. 28

    Calculating pi without arbitrary precision and only basic arithmetic

  29. 29

    Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

HotTag

Archive