Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Display floating point numbers in compact, fixed-width format

by particle (Vicar)
on Sep 26, 2003 at 14:01 UTC ( #294424=note: print w/replies, xml ) Need Help??


in reply to Display floating point numbers in compact, fixed-width format

I wanted something closer to "Engineering notation".... I'd like to be able to make the width a parameter (instead of fixed at 9). Being able to go as low as 7 (or even 6) would be way cool.

this code should meet those requirements. at least, the mantissa width is fixed, the exponent width varies. it should be easy enough to modify this code to suit your needs, if you wish.

i used your test suite, too.

#!/usr/bin/perl use strict; use warnings; $|++; Main(); exit; ## adapted from code found at: http://www.cs.tut.fi/~jkorpela/c/eng.ht +ml sub eng { my( $num, $digits )= @_; ## default to smallest number of digits allowing fixed width manti +ssa (4) $digits= defined $digits && 3 < $digits ? $digits : 4; my $neg; if( 0 > $num ) { $neg= 'true'; $num= -$num; } 0 == $num and return sprintf '+%.*fe+%s' => $digits - 1, $num, 0; my $exp= 0 != $num ## perl's log() is natural log, convert to common log ? int( log($num) / log(10) ) ## short-circuit: can't do log(0) : 0; ## tricky integer casting ahead... $exp= 0 < $exp ? int( ( int( $exp / 3 ) ) * 3 ) : int( int( ( -$exp + 3 ) / 3 ) * -3 ); $num *= 10 ** -$exp; if( 1000 <= $num ) { $num /= 1000; $exp += 3; } elsif( 100 <= $num ) { $digits -= 2; } elsif( 10 <= $num ) { $digits -= 1; } 0 <= $exp and $exp= '+' . $exp; return ( $neg ? '-' : '+' ) . sprintf '%.*fe%s' => $digits - 1, $num, $exp; } sub Main { my $digits= 2; for my $exp ( -101..-98, -11, -10..11, 98..101 ) { for my $sign ( '', '-' ) { my $num= 0 + ( $sign . "5.555555555e" . $exp ); printf "%-20s (%s)\n", $num, eng( $num, $digits ); } } for my $exp ( -10..11 ) { for my $sign ( '', '-' ) { my $num= 0 + ( $sign . "1e" . $exp ); printf "%-20s (%s)\n", $num, eng( $num, $digits ); printf "%-20s (%s)\n", 0, eng( 0, $digits ) if 1 == $num; } } }

~Particle *accelerates*

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://294424]
help
Chatterbox?
[marioroy]: Corion, no not intentionally. not at all.
[Lady_Aleena]: /ne tries to decide whther or not to take a hammer to hubby's alarm clock.
[Lady_Aleena]: He never hears it.
[marioroy]: Back in HighSchool, I came late to Gym class. There was a fellow who was picked last, no team wanted him for racketball. So it was us two against everybody. I looked at him in the eye and said to let your body loose and let the powers to be help us win.
[marioroy]: We made it all the way to the end. The expience was magical. I not good at sports. Someone or thing was there.
erix considers extracting a catalog ...
[karlgoethebier]: marioroy: Racket ball is considered harmful, like Squash
[marioroy]: Wait till folks see what MCE::Hobo can do in the upcoming release. Multiple instances, nested spawning, 3 levels, doesn't matter. it works on Windiows too.
[Lady_Aleena]: I hate sites that tell me to turn ad-blocker off. They should know better.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-05-29 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?