Solved

Perl precision arithmetic, typcasting?

Posted on 2011-03-09
4
573 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 9

Expert Comment

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

Author Comment

by:kindaprog
ID: 35089938
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
ID: 35090118
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
ID: 35090442
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Constant string is of type char *   ? 7 48
Powershell File Sort 8 45
Windows Server 2008 R2: %HOMEDRIVE%%HOMEPATH% Incorrect 4 46
Deploy restore partition 1 23
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

679 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