3

I have this program:

my $d = 40000 * 100 / 360;
print "At the equator\n";
printf "%9s° = %10.3f meters\n", 10**-$_, 10**-$_ * $d for 0 .. 7;

It outputs

At the equator (By the way, these are all off by a factor
of 10, but good thing that's not the point of the post.)
       1° =  11111.111 meters
     0.1° =   1111.111 meters
    0.01° =    111.111 meters
   0.001° =     11.111 meters
  0.0001° =      1.111 meters
   1e-05° =      0.111 meters
   1e-06° =      0.011 meters
   1e-07° =      0.001 meters

How can I fix my program so that exponential notation isn't used for the smaller numbers?

Dan Jacobson
  • 490
  • 3
  • 14
  • Does this answer your question? [What is c printf %f default precision?](https://stackoverflow.com/questions/1207762/what-is-c-printf-f-default-precision) – Jim Garrison Mar 20 '23 at 01:40
  • 2
    Why are you using `%s` to print a floating point number? This results in a default conversion of float to character, and the default precision (from the C library) is 6. Go beyond 6 and it switches to exponential notation. – Jim Garrison Mar 20 '23 at 01:43
  • 1
    Re "*Johnny turned to his elders again. But before he could utter a phrase, they said "RTFM" and slammed the door.*", That did not happen at all, [as everyone can see](https://stackoverflow.com/q/75773212/589924) – ikegami Mar 20 '23 at 02:57
  • (Not all of the Johnny story happened online.) – Dan Jacobson Mar 23 '23 at 07:32

1 Answers1

9
printf "%9.${_}f° = %9.3f meters\n", 10**-$_, 10**-$_ * $d for 0 .. 7;

or

printf "%9.*f° = %9.3f meters\n", $_, 10**-$_, 10**-$_ * $d for 0 .. 7;

Output:

        1° = 11111.111 meters
      0.1° =  1111.111 meters
     0.01° =   111.111 meters
    0.001° =    11.111 meters
   0.0001° =     1.111 meters
  0.00001° =     0.111 meters
 0.000001° =     0.011 meters
0.0000001° =     0.001 meters
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Directly using the exponent as the length, nice! Very tautologically satisfying. – Jim Davis Mar 20 '23 at 13:23
  • OK, that is very clever. However what if the input is not in predictable order: .000001, .0001, .00001 ? – Dan Jacobson Mar 21 '23 at 00:25
  • Each loop pass is completely independent of the other, so the output can be in any order you want. For your example, you'd change `0 .. 7` to `6, 4, 5`. – ikegami Mar 21 '23 at 00:28
  • How can I make each line have two of the same?: for (qw/0.000001 0.0001 0.00001/) { print $_, " ", abs, "\n"; } ... without doubling the size of the program? – Dan Jacobson Mar 21 '23 at 00:35
  • 1
    I have no idea what any of that means. You're proving why the comments are not the place for new questions. [Go here](https://stackoverflow.com/questions/ask). Make sure you explain your question clearly, including any inputs, the output your attempt gives, and the output you desire. – ikegami Mar 21 '23 at 00:40
  • https://stackoverflow.com/questions/75797326/how-to-force-perl-to-print-small-numbers-in-the-same-format-as-other-numbers/75831019#75831019 – Dan Jacobson Mar 25 '23 at 10:27
  • That would be an awful solution to the qestion asked here. – ikegami Mar 25 '23 at 15:28