Solved

Perl precision arithmetic, typcasting?

Posted on 2011-03-09
4
569 Views
Last Modified: 2013-12-25
Thinking out loud here... just need some assurance and explanation.

I ran across code like this today.

$number = 100.12;
$number = int($number*100);
print "$number\n";
$number = $number/100;
print "$number\n";

Yields:
10012
100.12

as expected....

The problem: 1200.12
Yields:
120011
1200.11

So I think the only issue is the int(). I removed it and so far results are as expected. I'm just surprised that I found this issue, the code has been in use for a long time. So I'm concerned that there was a good reason for it and I'm creating another unforeseen problem. Also, want to make sure there's just not a better way to do it.

The value should always be a whole dollar or dollar.cents value, and should be enforced as such. Then stored without the decimal.

So (I guess) my question is what would you do?

The objective is to store the number without the decimal in it. The code snip is just an adhoc example. I mean you could do it without even using math. I know Perl has issues with floating-point arithmetic precision.
 
0
Comment
Question by:kindaprog
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
What about using the string?
0
 

Author Comment

by:kindaprog
Comment Utility
Do you mean just checking if it has a decimal point?

Then if it does split on it and make sure the right side has two digits (add a zero if needed etc.) and cat the two parts back together... if it doesn't just tack on two zeros.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
Comment Utility
Perl doesn't have an issue with floating point.  It's common to all programming languages.

int EXPR
       int     Returns the integer portion of EXPR.  If EXPR is omitted, uses
               $_.  You should not use this function for rounding: one because
               it truncates towards 0, and two because machine representations
               of floating point numbers can sometimes produce
               counterintuitive results.  For example, "int(-6.725/0.025)"
               produces -268 rather than the correct -269; that's because it's
               really more like -268.99999999999994315658 instead.  Usually,
               the "sprintf", "printf", or the "POSIX::floor" and
               "POSIX::ceil" functions will serve you better than will int().
0
 

Author Comment

by:kindaprog
Comment Utility
Yes Sir. I was just realizing this fact. Same results in a c program...

Do you have a preference/recommendation??

I was just trying this and it seems to work consistently.

$number = sprintf("%.0f", $number)

Thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PowerShell Get-Help 10 59
How to count json data 2 56
Ned Perl Snippet to Read Files in Directory 5 37
How can I do this in Pyhton? 12 66
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now