fixed, scientific and engineering notation as awk function 
Author Message
 fixed, scientific and engineering notation as awk function

Hi,

I'm looking for a function that has as input:

  -a number to be displayed in a certain way
  -a string or number specifying wether fixed, scientific or engineering
   notation schould be used (like on your pocket calculator)
  -a number specifying the precision

The function schould generate as output :

  -a string displaying the number in the desired way.

---------------------------------------------

The difference between fixed, scientific and engineering notation is
given below:

-Engineering notation represents numbers in an exponential form where
 the powers are allowed to be multiples of 3.
   eng_4(123.45e+3) = "123.5e3"
   eng_7(123.45e+3) = "123.45e3"
   eng_4(23.45e-5)  = "234.5e-6"
 In fact, for the engineering notation, I would like to have an option
 to specify wether the power must be represented numerically or with a
 letter if this is possible
   e-18 = a
   e-15 = f
   e-12 = p
   e-9  = n
   e-6  = u
   e-3  = m
   e+3  = k
   e+6  = M
   e+9  = G
   e+12 = T
   e+18 = E
 Powers equal to zero should be ommited (e.g. eng_3(1.23)="1.23")
 since their info carriage is zip.

-Scientific notation: all integers are acceptable as power.
 Note that I want the trailing zeros chopped off. Also required
 is that if not all significant digits can be displayed, the last
 number in the mantisse must be rounded correctly. (The last two
 remarks also apply to engineering notation)
   sci_7(45.97)    = "4.597e1"
   sci_2(45.97e-7) = "4.6e-6"

-Fixed notation: all numbers are displayed without exponent as long
 as they can be displayed this way. If this is no longer the case,
 (the maximum numbers in the display string (e.g. 10) are exeeded),
 the notation switches automatically to scientific notation with the
 same precision.

---------------------------------------------------------

I already looked in the AWK FAQ and the gawk users manual (library
part), but don't find such a routine.

Has anyone already encountered/written such a routine?

Best regards,

FL

-----------------------------------------------------------------------------
 Francky Leyn                          
 K.U.Leuven - ESAT MICAS            E-mail :

 Kardinaal Mercierlaan 94 - 91.21   Tel    : ++32 - (0)16  32.10.85
 B-3001 Heverlee - Belgium          Fax    : ++32 - (0)16  32.19.75
-----------------------------------------------------------------------------



Mon, 06 Mar 2000 03:00:00 GMT  
 fixed, scientific and engineering notation as awk function

Well, this gets you most of the way there.  It's up to you to extend
it so you can have G/M/K/m/u/etc. for engineering notation.  Shouldn't
be hard.

# Scientific notation.  Number is displayed as D.DDDeN where first two
# digits are not both zero.

function fmt_sci(n, L, R,  a, s, d, df) {

        a = abs(n)
        s = sgn(n)
        d = int(log(a)/log(10))
        df = 10^(-d)

        return short(s*n*df, L, R) emaybe(d)

Quote:
}

# Engineering notation.  Number is displayed as [DD]D.DDDeN where
# first two digits are not both zero and N is a multiple of 3.

function fmt_eng(n, R,  a, s, d, df) {

        a = abs(n)
        s = sgn(n)
        for(d = int(log(a)/log(10)); d % 3 != 0; d--);
        df = 10^(-d)

        return short(s*n*df, 3, R) emaybe(d)

Quote:
}

function abs(n) {

        return n < 0 ? -n : n;

Quote:
}

function sgn(n) {

        return n < 0 ? -1 : 1;

Quote:
}

function short(n, L, R) {

        return strip0s(sprintf("%"L"."R"f", n))

Quote:
}

function strip0s(s) {

        sub(/0*$/, "", s)

        return s

Quote:
}

function emaybe(e) {

        return e != 0 ? "e"e : ""

Quote:
}

{ print fmt_sci($0, 3, 3) "    " fmt_eng($0, 2) }

--



Mon, 06 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. AWK function for fixed, scientific and engineering notation

2. Converting in-fix notation to Post-fix

3. reading scientific notation?

4. Scientific Notation

5. Datatype() Wholenumber scientific notation fails ?

6. scientific notation in Rexx?

7. What happened to Scientific Notation

8. scientific notation

9. regexp: how to catch numbers in scientific notation?

10. Scientific/engineering computing

11. SYSTEM/EXTENSIONS (was Re: Scientific/engineering com)

 

 
Powered by phpBB® Forum Software