Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > significant figures

Reply
Thread Tools

significant figures

 
 
dan
Guest
Posts: n/a
 
      01-05-2010
Whilst trying to create something that would parse a number into one with
an appropriate number of significant figures, I accidentally wrote this:

sub sigfig {
my ($sigfigs, $number) = @_;

my $divisor = 10**(length(int $number) - $sigfigs);
$number /= $divisor;
$number = sprintf "%1.0f", $number;
$number *= $divisor;

return $number
}

which seems to work for positive numbers not in scientific notation.

As my friends and colleagues consider me big-headed, I would appreciate
it if somebody could point out any inadequacies,foibles and associated
notwithstandings of the above code, bringing me down a much needed peg or
to.

dan
 
Reply With Quote
 
 
 
 
Jochen Lehmeier
Guest
Posts: n/a
 
      01-06-2010
On Tue, 05 Jan 2010 22:38:41 +0100, dan <> wrote:

> Whilst trying to create something that would parse a number into one with
> an appropriate number of significant figures, I accidentally wrote this:


> sub sigfig {
> my ($sigfigs, $number) = @_;


$number = sprintf("%d",$number); # just in case...
return $number if if ($sigfgs > length($number);

> my $divisor = 10**(length(int $number) - $sigfigs);
> $number /= $divisor;
> $number = sprintf "%1.0f", $number;


# instead:
$number = sprintf("%d",$number);

> $number *= $divisor;
>
> return $number
> }


> inadequacies,foibles and associated notwithstandings of the above code,


Well, if it works, it works.

A bit shorter:

$number = sprintf("%d",$number); # just in case...
return substr($number,0,$sigfigs) . ("0" x (length($number) -
$sigfigs));

 
Reply With Quote
 
 
 
 
Jochen Lehmeier
Guest
Posts: n/a
 
      01-07-2010
On Wed, 06 Jan 2010 21:52:51 +0100, John Stanley <> wrote:

> $number = sprintf("%d", $number); # just in case ...
> return $number if $sigfigs > length($number);
>
> shows an even more amazing lack of understanding of significant figures.
> It would return "1" for sigfigs 4, 1.234, which is patently absurd.


That would be me.

I'm sorry, English is not my primary language. I was not aware that
"significant figure" has a particular meaning (and yes, Google shows it
immediately), so misinterpreted his question. I thought his method was
just for integer numbers, i.e. sigfigs(4,123456)=123400 etc..
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-11-2010
On Wed, 6 Jan 2010 12:52:51 -0800, John Stanley <> wrote:

>
>On Wed, 6 Jan 2010, Ben Morrow wrote:
>> Quoth dan <>:
>>> Whilst trying to create something that would parse a number into one with
>>> an appropriate number of significant figures, I accidentally wrote this:
>>>
>>> sub sigfig {
>>> my ($sigfigs, $number) = @_;
>>>
>>> my $divisor = 10**(length(int $number) - $sigfigs);
>>> $number /= $divisor;
>>> $number = sprintf "%1.0f", $number;
>>> $number *= $divisor;
>>>
>>> return $number
>>> }
>>>
>>> which seems to work for positive numbers not in scientific notation.

>>
>> It fails for cases like
>>
>> sigfigs 2, 0.00123;
>>
>> Ben

>
>And for sigfigs 7, 123
>
>The auto-conversion of numbers to strings and back in perl makes it
>difficult to manage significant figures without keeping that information
>separately. Even something simple like:
>
>$number = "123.0000";
>print "$number\n";
>print $number + 1.0000 . "\n";
>print "$number" + "1.0000" . "\n";
>
>shows the loss of information about significance. The only printed result
>that is correct wrt sig-figs is the first, and that's only because $number
>started as a string and was printed as a string. In the latter two cases,
>the addition forced the conversion.


$number started as a string and ended as a string.
You should assume that general rules of temporaries still apply ..
even in Perl. In fact, in your above example, $number was never
alterred, just assigned once.
There was no conversion of $number at all as this proves:
----
my $number = "123.0000";
print "$number\n";
print $number + 1.0000 . "\n";
print "$number" + "1.0000" . "\n";
print "Nope, still a string -> ", $number, "\n";
$number += "0.0000";
print "Now its a number -> ", $number, "\n";
----
123.0000
124
124
Nope, still a string -> 123.0000
Now its a number -> 123
----

I don't care what language your in, source code
constants at compile time, are converted to either a number or a string.
"123.0000" is a char(10), and 123.0000 is a float().
In eazy Perl pseudo, its the same as
struct {
char *c;
float f;
... more
TYPE last;
} number;

Temporaries are still the same and conversions only
happen on assignments.
I don't think its too tricky to understand that numeric
operations should be homogenous, and print is just a conversion,
accurate or not, of a temporary, a snapshot of the variable,
in space and time, that has absolutely nothing to do with assigning
to the variable.

There is nothing extrordinary about Perl in that regard, same numeric
base conversion problems as any other language.

In the OP's example code, assuming $number was passed in as a number,
then this line: $number = sprintf "%1.0f", $number;
is a problem. But it would be no less a problem if done in C++:
number = atof ( sprintf( "%1.0f", number) );
This does not maintain numeric integrity in any language.

-sln
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-12-2010
On Mon, 11 Jan 2010 20:10:45 -0800, John Stanley <> wrote:

>On Mon, 11 Jan 2010, wrote:
>
>> On Wed, 6 Jan 2010 12:52:51 -0800, John Stanley <> wrote:
>>
>>>
>>> The auto-conversion of numbers to strings and back in perl makes it
>>> difficult to manage significant figures without keeping that information
>>> separately. Even something simple like:

>>
>> $number started as a string and ended as a string.

>
>I did not say that the contents of $number changed without an assignment,
>I said that the value was converted from string to numeric as necessary.
>
>> I don't think its too tricky to understand that numeric
>> operations should be homogenous, and print is just a conversion,
>> accurate or not, of a temporary, a snapshot of the variable,
>> in space and time, that has absolutely nothing to do with assigning
>> to the variable.

>
>I didn't say it was tricky to understand, and I didn't say that print
>assigned anything to anything. 'Print' was there only to show the result
>of the operations being performed on the values.
>
>> There is nothing extrordinary about Perl in that regard, same numeric
>> base conversion problems as any other language.

>
>Perl is unique in the sense that it will AUTOMATICALLY convert from string
>to number when it is performing operations that require it. Other
>languages, at least those I am familiar with, require the programmer to
>know which is which and convert as required.
>

I'm sure there are other typeless languages out there.

>While there was no actual conversion of the stored values in the code I
>wrote, it was trivial demo code showing the loss of information by the
>operations themselves, and you would expect real code would have a few
>assignments saving the incorrect results.


Yes, I agree entirely. My point was that real code involving
numeric calculations won't mix string/number conversions except on entry
points or initialization, and coerce to a number if its a typeless language
if the language supports that. Even in Perl you can do that if precautions
are taken.

So significance is ludicrous in the OP's context of conversion, then assignment.
Precision is everything, the least precise number as it interracts with calculations,
is the limiting factor of significance. Significance is secondary.

I wouldn't use Perl as a numerical methods language, but it could be done.
For engineers/math types usually thier first project in Perl is to try to
convert some numeric project, and the freewheeling conversions are a trap for them.
I think the term "automatic conversion" is a misnomer since clearly one can
usually point to the offending line of code involving string assignment or
cataenation/assignment operators. Once the string side is breaced, the variable
is tainted with respect to numeric calculations. There should only be one way
conversions, number -> temporary -> string -> print. Entry points are a different
issue.

-sln
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      01-13-2010
>>>>> "Shmuel" == Shmuel (Seymour J ) Metz <> writes:

>> Perl is unique in the sense that it will AUTOMATICALLY convert from
>> string to number


Shmuel> Rexx, which is older than Perl. "There may be many others but they haven't
Shmuel> been discovered."

I believe Awk also did this just fine. Does Rexx predate Awk?

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Significant figures calculation Harold Python 16 06-28-2011 08:47 PM
Significant Digits (Significant Figures) SMH Javascript 0 01-07-2007 09:52 AM
Re: Round to significant figures (C++) Alf P. Steinbach C++ 0 05-01-2006 09:15 AM
more than 16 significant figures Jeremy Watts Java 32 07-15-2005 02:21 PM
round up to nearest number and significant figures Steve Java 5 05-17-2004 01:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57