Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3237720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:46:17+00:00 2026-05-17T17:46:17+00:00

How do I fix this code so that 1.1 + 2.2 == 3.3? What

  • 0

How do I fix this code so that 1.1 + 2.2 == 3.3? What is actually happening here that’s causing this behavior? I’m vaguely familiar with rounding problems and floating point math, but I thought that applied to division and multiplication only and would be visible in the output.

[me@unixbox1:~/perltests]> cat testmathsimple.pl 
#!/usr/bin/perl

use strict;
use warnings;

check_math(1, 2, 3);
check_math(1.1, 2.2, 3.3);

sub check_math {
        my $one = shift;
        my $two = shift;
        my $three = shift;

        if ($one + $two == $three) {
                print "$one + $two == $three\n";
        } else {
                print "$one + $two != $three\n";
        }
}

[me@unixbox1:~/perltests]> perl testmathsimple.pl 
1 + 2 == 3
1.1 + 2.2 != 3.3

Edit:

Most of the answers thus far are along the lines of “it’s a floating point problem, duh” and are providing workarounds for it. I already suspect that to be the problem. How do I demonstrate it? How do I get Perl to output the long form of the variables? Storing the $one + $two computation in a temp variable and printing it doesn’t demonstrate the problem.

Edit:

Using the sprintf technique demonstrated by aschepler, I’m now able to “see” the problem. Further, using bignum, as recommended by mscha and rafl, fixes the problem of the comparison not being equal. However, the sprintf output still indicates that the numbers aren’t “correct”. That’s leaving a modicum of doubt about this solution.

Is bignum a good way to resolve this? Are there any possible side effects of bignum that we should look out for when integrating this into a larger, existing, program?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:46:17+00:00Added an answer on May 17, 2026 at 5:46 pm

    See What Every Computer Scientist Should Know About Floating-Point Arithmetic.

    None of this is Perl specific: There are an uncountably infinite number of real numbers and, obviously, all of them cannot be represented using only a finite number of bits.

    The specific “solution” to use depends on your specific problem. Are you trying to track monetary amounts? If so, use the arbitrary precision numbers (use more memory and more CPU, get more accurate results) provided by bignum. Are you doing numeric analysis? Then, decide on the precision you want to use, and use sprintf (as shown below) and eq to compare.

    You can always use:

    use strict; use warnings;
    
    check_summation(1, $_) for [1, 2, 3], [1.1, 2.2, 3.3];
    
    sub check_summation {
        my $precision = shift;
        my ($x, $y, $expected) = @{ $_[0] };
        my $result = $x + $y;
    
        for my $n ( $x, $y, $expected, $result) {
            $n = sprintf('%.*f', $precision, $n);
        }
    
        if ( $expected eq $result ) {
            printf "%s + %s = %s\n", $x, $y, $expected;
        }
        else {
            printf "%s + %s != %s\n", $x, $y, $expected;
        }
        return;
    }
    

    Output:

    1.0 + 2.0 = 3.0
    1.1 + 2.2 = 3.3
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is probably is simple fix for this but I currently have code similar
Even after all the hotfixes and updates that are supposed to fix this, my
I've been trying to fix this for two hours straight and I can't figure
Ok I want some opinions how I can fix this mess of a method!
Does anyone have an idea how can I fix this vulnerability in Apache 2.2.4
This may be a simple fix - but I'm trying to sum together all
This is a problem I'm sure is easy to fix, but I've been banging
This sure seems like a simple error to fix. However, I somehow can't figure
Could you help me fix the menu on this this page ? The menu
I want to fix a file in past commit. This might affect all ascending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.