Re: trouble with math... driving me nuts.

2003-08-22 Thread zsdc
[EMAIL PROTECTED] wrote:

>>#!/usr/bin/perl -wl
>>use Math::BigFloat;
>>$x = 37.75;
>>print $x - 33.67 - 4.08;
>>$x = Math::BigFloat->new('37.75');
>>print $x - 33.67 - 4.08;
This seems to work, although there are some odd line returns
You mean the automatic "\n" with every print? It's because of the -l 
switch in the #! line. See perlrun(1) manpage:
http://www.perldoc.com/perl5.8.0/pod/perlrun.html#-l%5boctnum%5d

I like to use it in simple programs, as it makes them cleaner without 
all of those explicit newlines, but you can just remove the -l switch 
and have a standard print behavior. (Keep the -w, however, it turns on 
warnings. Or better yet use "use warnings;" pragma.)

and '0' is being returned as '0.'
Still, I can work around that, THANKS!
Yes, indeed. I checked out the source of Math::BigFloat and 
unfortunately the class name is hardcoded all over the place (it's 
probably fixed in newer versions) so it's hard to subclass, but this is 
Perl, after all, so we can redefine the overloaded stringification 
operator in the Math::BigFloat class itself:

use Math::BigFloat;
{
package Math::BigFloat;
use overload '""' => sub {(my $n = $_[0]->stringify) =~ s/\.$//; $n};
}
Now it prints just "0" without the "." at the end. But note that such a 
code will stop working if the stringify method in Math::BigFloat is ever 
renamed. Unlikely, but possible. It's probably not the most elegant way 
to program, still it's pretty cool.

You could store the \&{'Math::BigFloat::(""'} reference to have the 
original stringification method even if it's renamed (anyone knows if 
it's documented somewhere?) but it could introduce another problems with 
overload.pm implementation.

The code I wrote gives a warning about the ("" subroutine being 
redefined in overload.pm when the script is run with the -w switch, but 
(at least on my system) it doesn't print the warning if there's "use 
warnings;" instead of -w, which is better anyway.

-zsdc.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: trouble with math... driving me nuts.

2003-08-22 Thread Peter_Farrar

True, it's not a Perl issue (I've been able to duplicate the problem in C,
and Scheme), but I'm looking for a Perl solution.  Math::BigFloat seems to
work well enough.

Thanks,
Peter



|-+>
| |   "Levon Barker"   |
| |   <[EMAIL PROTECTED]>  |
| ||
| |   08/21/2003 03:09 |
| |   PM   |
| ||
|-+>
  
>--|
  |
  |
  |   To:   [EMAIL PROTECTED], [EMAIL PROTECTED]   
|
  |   cc:  
      |
  |   Subject:  RE: trouble with math... driving me nuts.  
  |
  
>--|




Hi Peter,

This is a floating point issue. It is a general computing problem and not
just subject to Perl. In decimal form the result
is -0.0017763568.

Generally thats usually acurate enough. Otherwise you could truncate it or
round it to the nearest quadrabillionth.

Cheers,
Levon Barker

> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Sent: Thursday, August 21, 2003 3:06 PM
> To: [EMAIL PROTECTED]
> Subject: trouble with math... driving me nuts.
>
>
> Luckily I was easily able to recreate the problem.  See code below:
>
>   print 37.75 - 33.67 - 4.08 ;
>   ;
>
> I find these things all the time.  Is there a particular module I can use
> to fix these things?
>
> Output is
>
>   -1.77635683940025e-015
>
> Should be 0
>
> Running on Win2000 / Intel P3
>
> -Peter
>
>
> ** CONFIDENTIALITY NOTICE **
> NOTICE:  This e-mail message and all attachments transmitted with it may
> contain legally privileged and confidential information intended
> solely for
> the use of the addressee.  If the reader of this message is not the
> intended recipient, you are hereby notified that any reading,
> dissemination, distribution, copying, or other use of this message or its
> attachments is strictly prohibited.  If you have received this message in
> error, please notify the sender immediately and delete this message from
> your system.  Thank you.
>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>





** CONFIDENTIALITY NOTICE **
NOTICE:  This e-mail message and all attachments transmitted with it may
contain legally privileged and confidential information intended solely for
the use of the addressee.  If the reader of this message is not the
intended recipient, you are hereby notified that any reading,
dissemination, distribution, copying, or other use of this message or its
attachments is strictly prohibited.  If you have received this message in
error, please notify the sender immediately and delete this message from
your system.  Thank you..




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: trouble with math... driving me nuts.

2003-08-22 Thread Peter_Farrar

This seems to work, although there are some odd line returns and '0' is
being returned as '0.'
Still, I can work around that, THANKS!



|-+>
| |   zsdc <[EMAIL PROTECTED]>|
| ||
| |   08/21/2003 03:28 |
| |   PM   |
| ||
|-+>
  
>--|
  |
  |
  |   To:   [EMAIL PROTECTED]  
 |
  |   cc:   [EMAIL PROTECTED]  
     |
  |   Subject:  Re: trouble with math... driving me nuts.  
  |
  
>--|




[EMAIL PROTECTED] wrote:

>   print 37.75 - 33.67 - 4.08 ;
>   ;
>
> I find these things all the time.  Is there a particular module I can use
> to fix these things?

Take a look at Math::BigFloat, it's an arbitrary length float math package:

   #!/usr/bin/perl -wl
   use Math::BigFloat;
   $x = 37.75;
   print $x - 33.67 - 4.08;
   $x = Math::BigFloat->new('37.75');
   print $x - 33.67 - 4.08;

> ** CONFIDENTIALITY NOTICE **
> NOTICE:  This e-mail message and all attachments transmitted with it may
> contain legally privileged and confidential information intended solely
for
> the use of the addressee.

OK, I won't tell anyone.

-zsdc.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





** CONFIDENTIALITY NOTICE **
NOTICE:  This e-mail message and all attachments transmitted with it may
contain legally privileged and confidential information intended solely for
the use of the addressee.  If the reader of this message is not the
intended recipient, you are hereby notified that any reading,
dissemination, distribution, copying, or other use of this message or its
attachments is strictly prohibited.  If you have received this message in
error, please notify the sender immediately and delete this message from
your system.  Thank you..




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: trouble with math... driving me nuts.

2003-08-21 Thread zsdc
[EMAIL PROTECTED] wrote:

  print 37.75 - 33.67 - 4.08 ;
  ;
I find these things all the time.  Is there a particular module I can use
to fix these things?
Take a look at Math::BigFloat, it's an arbitrary length float math package:

  #!/usr/bin/perl -wl
  use Math::BigFloat;
  $x = 37.75;
  print $x - 33.67 - 4.08;
  $x = Math::BigFloat->new('37.75');
  print $x - 33.67 - 4.08;
** CONFIDENTIALITY NOTICE **
NOTICE:  This e-mail message and all attachments transmitted with it may
contain legally privileged and confidential information intended solely for
the use of the addressee.
OK, I won't tell anyone.

-zsdc.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: trouble with math... driving me nuts.

2003-08-21 Thread Levon Barker
Hi Peter,

This is a floating point issue. It is a general computing problem and not
just subject to Perl. In decimal form the result
is -0.0017763568.

Generally thats usually acurate enough. Otherwise you could truncate it or
round it to the nearest quadrabillionth.

Cheers,
Levon Barker

> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Sent: Thursday, August 21, 2003 3:06 PM
> To: [EMAIL PROTECTED]
> Subject: trouble with math... driving me nuts.
>
>
> Luckily I was easily able to recreate the problem.  See code below:
>
>   print 37.75 - 33.67 - 4.08 ;
>   ;
>
> I find these things all the time.  Is there a particular module I can use
> to fix these things?
>
> Output is
>
>   -1.77635683940025e-015
>
> Should be 0
>
> Running on Win2000 / Intel P3
>
> -Peter
>
>
> ** CONFIDENTIALITY NOTICE **
> NOTICE:  This e-mail message and all attachments transmitted with it may
> contain legally privileged and confidential information intended
> solely for
> the use of the addressee.  If the reader of this message is not the
> intended recipient, you are hereby notified that any reading,
> dissemination, distribution, copying, or other use of this message or its
> attachments is strictly prohibited.  If you have received this message in
> error, please notify the sender immediately and delete this message from
> your system.  Thank you.
>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: trouble with math... driving me nuts.

2003-08-21 Thread Peter_Farrar

Hi Bob,

I'm doing data-processing (EDI).  I need to format and present values in
text, round here and there.  Have lots of various attempts that have failed
for one math related reason or another.  Currently I convert to a string
and split and round, etc. (Even modulo '%' has failed me at times).  So
this 0 comes across as -1.78.  Tried printf("%.02f", $value) and got -0.00
back (negative 0?), so I'm not trusting that route much.

The documentation you refered me to (which didn't work on 2000, but luckily
I keep my Linux laptop near me at all times) refers to Math::BigFloat.  If
you can think of a better package I'd go that route though.

Thanks,
Peter

P.S. Things are a little crazy here, so I'm getting chatty.  Sorry if
there's too many words above.



|-+--->
| |   "Bob Showalter" |
| |   <[EMAIL PROTECTED]|
| |   rwhite.com> |
| |   |
| |   08/21/2003 03:18 PM |
| |   |
|-+--->
  
>--|
  |
  |
  |   To:   "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>, [EMAIL PROTECTED]   
   |
  |   cc:              
  |
  |   Subject:  RE: trouble with math... driving me nuts.  
  |
  
>--|




[EMAIL PROTECTED] wrote:
> Luckily I was easily able to recreate the problem.  See code below:
>
>   print 37.75 - 33.67 - 4.08 ;
>   ;
>
> I find these things all the time.  Is there a particular
> module I can use
> to fix these things?
>
> Output is
>
>   -1.77635683940025e-015
>
> Should be 0

That is zero, within the limits of the precision of floating point numbers.

Read the faq article:

   perldoc -q 'long decimals'

There are a number of modules on CPAN that go beyond the FAQ to address
this
issue. What are you trying to do?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





** CONFIDENTIALITY NOTICE **
NOTICE:  This e-mail message and all attachments transmitted with it may
contain legally privileged and confidential information intended solely for
the use of the addressee.  If the reader of this message is not the
intended recipient, you are hereby notified that any reading,
dissemination, distribution, copying, or other use of this message or its
attachments is strictly prohibited.  If you have received this message in
error, please notify the sender immediately and delete this message from
your system.  Thank you..




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: trouble with math... driving me nuts.

2003-08-21 Thread Bob Showalter
[EMAIL PROTECTED] wrote:
> Luckily I was easily able to recreate the problem.  See code below:
> 
>   print 37.75 - 33.67 - 4.08 ;
>   ;
> 
> I find these things all the time.  Is there a particular
> module I can use
> to fix these things?
> 
> Output is
> 
>   -1.77635683940025e-015
> 
> Should be 0

That is zero, within the limits of the precision of floating point numbers.

Read the faq article:

   perldoc -q 'long decimals'

There are a number of modules on CPAN that go beyond the FAQ to address this
issue. What are you trying to do?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



trouble with math... driving me nuts.

2003-08-21 Thread Peter_Farrar
Luckily I was easily able to recreate the problem.  See code below:

  print 37.75 - 33.67 - 4.08 ;
  ;

I find these things all the time.  Is there a particular module I can use
to fix these things?

Output is

  -1.77635683940025e-015

Should be 0

Running on Win2000 / Intel P3

-Peter


** CONFIDENTIALITY NOTICE **
NOTICE:  This e-mail message and all attachments transmitted with it may
contain legally privileged and confidential information intended solely for
the use of the addressee.  If the reader of this message is not the
intended recipient, you are hereby notified that any reading,
dissemination, distribution, copying, or other use of this message or its
attachments is strictly prohibited.  If you have received this message in
error, please notify the sender immediately and delete this message from
your system.  Thank you.




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]