Re: Variable division, assignment and sprintf in one line



From: Steve Bertrand <iaccounts@xxxxxxxxxx>
$var = sprintf ("%.2f", (($var /=1024) /=1024)))

What the ...?!?

/= ?

It was an off the top of my head example of what I wanted to do. Without
being at the code at the time, I was simply thinking +=.

If you want to divide something use /. If you want to divide a
variable and store the result in a variable use /=. In either case /=
or += or any op= deep inside an expression is a red flag. Especially
if you do that twice to the same variable and in the end assign the
value of the statement to the very same variable. An error waiting to
happen.

What if you later decided that you actually want the formatted size
converted by megabytes (or megawhatever) in a different variable and
just changed the

$var = sprintf ("%.2f", (($var /=1024) /=1024)))

to

$formatted = sprintf ("%.2f", (($var /=1024) /=1024)))

? In that case the $var would still be modified. Simply don't use /=
if all you need is /.

It's similar to writing something like

$x = $x++ + ++$x;

Now what does that mean? Or what does

$x = ($x /= 48) + ($x *= 1.78);

mean?

You divide the value of $var by 1024, assign it back to $var, divide
it by another 1024, assign it again to $var, then format the
resulting number using sprinf() and assign it to $var?!? Why???

Why? Because I am not a professional, hence why I am asking at
'beginners' ;)

$var = sprintf ("%.2f", $var / (1024*1024));

And it will be more efficient because the multiplication will be done
at compile time (once) and you'll only do a single division each
time.

I did not know that multiplication would be done at compile time. Is
this true for any mathematical equation that does not include a runtime
variable?

Yes, that's about right. Not all matematical functions are evaluated
during the optimization, but the basic ones are. You can test it by
something like this:

perl -MO=Deparse -e "print sin(43)"

The -MO=deparse instructs Perl to compile the code into its internal
form, optimize it and spit it out as formatted Perl code again.

Which is also more precise, though in this case it doesn't seem
to matter.

'twasn't about precision. It was a theoretical 'how can I do this?'.

Sure. That was just an unimportant remark in case you sometime later
needed to care about precision :-)

Oh, and furthermore, if you happened to read my original post, I had
asked how to perform what you state above, WITHOUT needing the left hand
side.

I happened to read not just your message but also the responses so I
knew that question has already been answered. You received two good
suggestions, to use a subroutine and to use for to alias the long
$var to $_ so there was nothing to add to that.

Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

.



Relevant Pages

  • Re: Minus Zero
    ... "Precision as Formatted". ... with a General format when I realized that "precision as displayed" ... It might eliminate the need to use ROUND for the result of each cell. ... the result in A4 is now exactly zero. ...
    (microsoft.public.excel)
  • Re: Custom Format Limitation
    ... Excel, like nearly all software, uses for format called "Double Precision Floating Point" to store numbers. ... This is an industry-wide standard, kept by the Institute Of Electrical And Electronic Engineers -- it is in now way unique to Excel or to Microsoft product in general. ...
    (microsoft.public.excel.misc)
  • Re: Read Format
    ... That statement as is just skips a line, as you have no I/O ... array of double precision type. ... what your problem isn't - namely in the I/O format. ... DOUBLE PRECISION:: tol, sum_e2 ...
    (comp.lang.fortran)
  • Re: C code for converting IBM/370 floating point to IEEE 754?
    ... point format to IEEE 754 single precision format. ... the IBM format has greater range but less precision than the ... IEEE format. ...
    (comp.lang.c)
  • Re: 24bits conversion
    ... Invalid precision. ... fread with '*uint8' and typecastthe resulting byte stream to 'uint16'. ... format is "little-endian". ... while incount < len ...
    (comp.soft-sys.matlab)