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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:57:45+00:00 2026-05-13T19:57:45+00:00

I have a simple routine which calculates the aspect ratio from a floating point

  • 0

I have a simple routine which calculates the aspect ratio from a floating point value. So for the value 1.77777779, the routine returns the string “16:9”. I have tested this on my machine and it works fine.

The routine is given as :

    public string AspectRatioAsString(float f)
    {
        bool carryon = true;
        int index = 0;
        double roundedUpValue = 0;
        while (carryon)
        {
            index++;
            float upper = index * f;

            roundedUpValue = Math.Ceiling(upper);

            if (roundedUpValue - upper <= (double)0.1 || index > 20)
            {
                carryon = false;
            }
        }

        return roundedUpValue + ":" + index;
    }

Now on another machine, I get completely different results. So on my machine, 1.77777779 gives “16:9” but on another machine I get “38:21”.

  • 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-13T19:57:46+00:00Added an answer on May 13, 2026 at 7:57 pm

    Here’s an interesting bit of the C# specifiction, from section 4.1.6:

    Floating-point operations may be
    performed with higher precision than
    the result type of the operation. For
    example, some hardware architectures
    support an “extended” or “long double”
    floating-point type with greater range
    and precision than the double type,
    and implicitly perform all
    floating-point operations using this
    higher precision type. Only at
    excessive cost in performance can such
    hardware architectures be made to
    perform floating-point operations with
    less precision, and rather than
    require an implementation to forfeit
    both performance and precision, C#
    allows a higher precision type to be
    used for all floating-point
    operations. Other than delivering more
    precise results, this rarely has any
    measurable effects.

    It is possible that this is one of the “measurable effects” thanks to that call to Ceiling. Taking the ceiling of a floating point number, as others have noted, magnifies a difference of 0.000000002 by nine orders of magnitude because it turns 15.99999999 into 16 and 16.00000001 into 17. Two numbers that differ slightly before the operation differ massively afterwards; the tiny difference might be accounted for by the fact that different machines can have more or less “extra precision” in their floating point operations.

    Some related issues:

    • C# XNA Visual Studio: Difference between "release" and "debug" modes?

    • CLR JIT optimizations violates causality?

    To address your specific problem of how to compute an aspect ratio from a float: I’d possibly solve this a completely different way. I’d make a table like this:

    struct Ratio
    {
        public int X { get; private set; }
        public int Y { get; private set; }
        public Ratio (int x, int y) : this()
        {
            this.X = x;
            this.Y = y;
        }
        public double AsDouble() { return (double)X / (double)Y; }
    }
    
    Ratio[] commonRatios = { 
       new Ratio(16, 9),
       new Ratio(4, 3), 
       // ... and so on, maybe the few hundred most common ratios here. 
       // since you are pinning results to be less than 20, there cannot possibly
       // be more than a few hundred.
    };
    

    and now your implementation is

    public string AspectRatioAsString(double ratio)      
    { 
        var results = from commonRatio in commonRatios
                      select new {
                          Ratio = commonRatio, 
                          Diff = Math.Abs(ratio - commonRatio.AsDouble())};
    
        var smallestResult = results.Min(x=>x.Diff);
    
        return String.Format("{0}:{1}", smallestResult.Ratio.X, smallestResult.Ratio.Y);
    }
    

    Notice how the code now reads very much like the operation you are trying to perform: from this list of common ratios, choose the one where the difference between the given ratio and the common ratio is minimized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you have a simple debounce routine handy to deal with a single switch
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple ASP.NET 3.5 SP1 Web Forms app... I've added the System.Web.Routing
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement
I develop tools in Autodesk Maya. Many of the tools I build have simple
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple 2-column layout with a footer that clears both the right
I have a login.jsp page which contains a login form. Once logged in the

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.