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

  • SEARCH
  • Home
  • 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 6586835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:50:30+00:00 2026-05-25T16:50:30+00:00

How do I calculate the aspect ratio (formatted as integer:integer ) by a given

  • 0

How do I calculate the aspect ratio (formatted as integer:integer) by a given factor?

For example, aspect ratio 16:9 has a factor of 1.778, because 16 / 9 = 1.778. But how can I find the ratio by that factor? So

Dimension getAspectRatio(double factor) {
    ...
}

public static void main(String[] arguments) {
    Dimension d = getAspectRatio(16d / 9d);
    System.out.println(d.width + ":" + d.height);
}

should return

16:9
  • 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-25T16:50:31+00:00Added an answer on May 25, 2026 at 4:50 pm

    Disclaimer: These algorithms are silly and inefficient. I’m sure there’s a better one…

    A silly, straightforward (not very efficient) algorithm to find an approximation is this:

    double ratio = 1.778;
    double bestDelta = Double.MAX_VALUE;
    int bestI = 0;
    int bestJ = 0;
    
    for (int i = 1; i < 100; i++) {
      for (int j = 1; j < 100; j++) {
        double newDelta = Math.abs((double) i / (double) j - ratio);
        if (newDelta < bestDelta) {
          bestDelta = newDelta;
          bestI = i;
          bestJ = j;
        }
      }
    }
    
    System.out.println("Closest ratio: " + bestI + "/" + bestJ);
    System.out.println("Ratio        : " + ((double) bestI / (double) bestJ));
    System.out.println("Inaccurate by: " + bestDelta); 
    

    Output.

    Closest ratio: 16/9
    Ratio        : 1.7777777777777777
    Inaccurate by: 2.2222222222234578E-4
    

    Update: Alternative algorithm

    I’ve just thought of an alternative algorithm, which tries to close in on the approximation. Of course, it’s still not very efficient…

    double bestDelta = Double.MAX_VALUE;
    int i = 1;
    int j = 1;
    int bestI = 0;
    int bestJ = 0;
    
    for (int iterations = 0; iterations < 100; iterations++) {
      double delta = (double) i / (double) j - ratio;
    
      // Optionally, quit here if delta is "close enough" to zero
      if (delta < 0) i++;
      else j++;
    
      double newDelta = Math.abs((double) i / (double) j - ratio);
      if (newDelta < bestDelta) {
        bestDelta = newDelta;
        bestI = i;
        bestJ = j;
      }
    }
    
    System.out.println("Closest ratio: " + bestI + "/" + bestJ);
    System.out.println("Ratio        : " + ((double) bestI / (double) bestJ));
    System.out.println("Inaccurate by: " + bestDelta);
    

    The output is the same

    Should I stumble upon an efficient algorithm, I’ll post it here 🙂

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

Sidebar

Related Questions

Is there a code analyzing utility that can do things like calculate the average
I am trying to calculate the difference between given two times and i find
Aloha, how do I determine/calculate whether an image aspect ratio is appropriate (proportion) in
How can I calculate the value of PI using C#? I was thinking it
I am developing an image analysis app and I need to calculate the aspect
I have N rectangular items with an aspect ratio Aitem (X:Y). I have a
Given a 2d picture of a rectangle distorted by perspective: I know that the
I want to calculate Saturday's and Sunday's in a date range? How can i
I am creating a page that has a table with a bunch of images
I want to calculate the variance in a dataset collected at a given time

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.