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 8606079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:00:36+00:00 2026-06-12T03:00:36+00:00

I’m writing an odds converter in PHP and came across this code for converting

  • 0

I’m writing an odds converter in PHP and came across this code for converting decimal odds to fractional ones

function dec2frac($dec) { 
    $decBase = --$dec; 
    $div = 1; 
    do { 
        $div++; 
        $dec = $decBase * $div; 
    } while (intval($dec) != $dec); 
    if ($dec % $div == 0) { 
        $dec = $dec / $div; 
        $div = $div / $div; 
    } 
    return $dec.'/'.$div; 
} 

When I tested the code, it would sometimes succeed in the calculations and sometime try to load the page for some time without success so I figured that it got stuck in the loop somehow. I set the time limit to 1 second and an echo in the loop, which confirmed my suspicion. I added these two echoes to the loop so I could see what went wrong.

echo "$dec $div<br/>";
echo var_dump(intval($dec) == $dec)." $dec is int<br/>";

Example printout when it fails, using decimal = 1.6

1.2 2
bool(false) 1.2 is int
1.8 3
bool(false) 1.8 is int
2.4 4
bool(false) 2.4 is int
3 5
bool(false) 3 is int //should break here and return 3/5
3.6 6
bool(false) 3.6 is int
4.2 7
bool(false) 4.2 is int

Example printout when it succeeds, using decimal = 1.8

1.6 2
bool(false) 1.6 is int
2.4 3
bool(false) 2.4 is int
3.2 4
bool(false) 3.2 is int
4 5
bool(true) 4 is int

Why doesn’t it recognize the integers sometimes? How can I fix it so it exits the loop when an integer is found?

  • 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-06-12T03:00:38+00:00Added an answer on June 12, 2026 at 3:00 am

    It looks like a floating precision rounding error. intval($dec) and $dec appear to be equal, but actually aren’t. The difference is negligible but strict equalness fails.

    On my 64bit system, with a precision of 1E-15 the function works, with a precision of 1E-16 it loops.

    In general, strict comparison of two floating point numbers is to be avoided. Two floats are to be considered “equal” if their difference is less than a threshold. There are ways of calculating this threshold (google for “determining machine precision”), for it is not the same everywhere.

    Since in my PHP.INI I have the default value

    ; The number of significant digits displayed in floating point numbers.
    ; http://php.net/precision
    precision = 14
    

    then the two numbers are shown equal even if one is 3 and the other 3.0000000000044, and the caltrop goes unnoticed.

    With precision = 18, $dec is shown not to be what you’d expect:

    1.20000000000000018
    1.80000000000000027
    2.40000000000000036
    3.00000000000000044
    

    Try:

    function dec2frac($dec) {
        $decBase = --$dec;
        $div = 1;
        do {
            $div++;
            $dec = $decBase * $div;
        } while (abs(intval($dec) - $dec) > 1e-15);
        if (($dec % $div) == 0) {
            $dec = $dec / $div;
            $div = $div / $div;
        }
        return $dec.'/'.$div;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to count the length of a string with PHP. The string

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.