AWK function for fixed, scientific and engineering notation
Author Message
AWK function for fixed, scientific and engineering notation

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.

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 such aroutine?

Best regards,

Francky

-----------------------------------------------------------------------------
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
-----------------------------------------------------------------------------

Sun, 05 Mar 2000 03:00:00 GMT
AWK function for fixed, scientific and engineering notation

Francky Leyn wrote, after having futilely RTFMed, so he says:

Quote:
> 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.

> 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 such aroutine?

Excuse me, but perhaps something like

sprintf("%*.*f",number,width,precision)

Want to try %e instead of %f to get ddd.ddEddd format?

Want to look again for the funny facts about printf() and sprintf()?

Hope that helps.

Sun, 05 Mar 2000 03:00:00 GMT
AWK function for fixed, scientific and engineering notation

Dear,

perhaps it is usefull to explain a little bit more in detail what the
difference is between fixed, scientific and engineering notation.

-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").

-Scientific notation: all integers are acceptable as power.
Note that I want the trailing zeros chopped. 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 of course 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.

Regards,

FL

Sun, 05 Mar 2000 03:00:00 GMT
AWK function for fixed, scientific and engineering notation

: 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.

I would like to see the same.  While this does not

function convert_to_eng(x) {
# calculate mantisa and exponent,
# convert to decimal and standard unit
# the returned object is always 10 characters long,
#   4 before decimal, decimal, and 5 after decimal
# example: -4.7399e+07 becomes " -47.399M "

forig=x
matches_found=match(x,/[Ee]/)
if (matches_found == 0) {
mantissa=forig
exponent=0
} else {
mantissa=substr(x,1,RSTART-1)
exponent=substr(x,RSTART+1)
}
factor=exponent%3
if ( factor<0 ) { factor +=3}
mantissa = mantissa*10^factor
exponent-=factor
foundunit=0
if (exponent ==  12 ) {unit="T";foundunit=1}
if (exponent ==   9 ) {unit="G";foundunit=1}
if (exponent ==   6 ) {unit="M";foundunit=1}
if (exponent ==   3 ) {unit="k";foundunit=1}
if (exponent ==   0 ) {unit=" ";foundunit=1}
if (exponent ==  -3 ) {unit="m";foundunit=1}
if (exponent ==  -6 ) {unit="u";foundunit=1}
if (exponent ==  -9 ) {unit="n";foundunit=1}
if (exponent == -12 ) {unit="p";foundunit=1}
if (exponent == -15 ) {unit="f";foundunit=1}

if ( foundunit ) {

if (match(mantissa,/[.]/)) {
whole=substr(mantissa,1,RSTART-1)
fraction=substr(mantissa,RSTART+1)
fraction=fraction "0000"
fraction=substr(fraction,1,4)
} else {
whole=mantissa
fraction="0000"
}

fnew=sprintf("%4s.%04d%1s", whole, fraction, unit)

return fnew
} else {
return forig
}

Quote:
} # function convert_to_eng(x)

Sat, 11 Mar 2000 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages