Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    2

    Unanswered: adding x.xxe-15 and x.xxe-20 in AWK

    Hi all

    I have a variable x where the value 0.85e-15 is read (from $NF) and stored in.

    Now I am reading the value 0.17e-20 from the file(also from $NF), and I have to add this value to x to finally display 8.500017e-16.

    When I am printing the value of x, it shows only the first value '0.85e-15', the second value never seems to get added.

    How do I do this in gAWK, if not, in C.

    Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The following awk function print a float number with all the needed digits after the decimal point (this number of digits can be limited) :

    Code:
    #
    # eprint - Print float number in "%e" format
    # Args:    number = number to print
    #          decmax = maximal number of digits after decimal point (def=6)
    # Output:  Number in exponential form, with number of digits after
    #          decimal point adjusted to the minimum required.
    #          1.123000e-5   => 1.123e-5
    #          8.500017e-16  => 8.500017e-16
    #
    
    function eprint(number, decmax,    tmp,dec) {
       tmp=sprintf("%." (decmax+0 == 0 ? 6 : decmax) "e",number);
       sub(/0*[eE][+-][0-9]+/,"",tmp);
       dec=length(tmp) - 2;
       dec = (dec < 1 ? 1 : dec);
       return sprintf("%." dec "e",number);
    }
    
    {
       print eprint($1,8);
    }'
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    2
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •