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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:31:18+00:00 2026-06-01T10:31:18+00:00

I have implemented an algorithm for floating point decimal to rational fraction approximation (example:

  • 0

I have implemented an algorithm for floating point decimal to rational fraction approximation (example: 0.333 -> 1/3) and now I wonder, is there a way to find an irrational number which satisfies the condition. For example, given the input 0.282842712474 I want the result to be sqrt(2)/5 and not 431827/1526739 which my algorithm produces. The only condition is that the first digits of the result (converted back to floating point) should be the digits of the input, the rest doesn’t matter. Thanks in advance!

  • 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-01T10:31:20+00:00Added an answer on June 1, 2026 at 10:31 am

    I came up with solution, that from given set of possible denominators and nominators finds best approximation of given number.

    For example this set can contain all numbers that can be created by:
    1 <= radicand <= 100000
    1 <= root_index <= 20

    If set has N elements, than this solution finds best approximation in O(N log N).

    In this solution X represents denominator and Y nominator.

    1. sort numbers from set
    2. for each number X from set:
      using binary find smallest Y such that Y/X >= input_number
      compare Y/X with currently best approximation of input_number

    I couldn’t resist and I implemented it:

    #include <cstdio>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    
    struct Number {
      // number value
      double value;
    
      // number representation
      int root_index;
      int radicand;
    
      Number(){}
      Number(double value, int root_index, int radicand)
        : value(value), root_index(root_index), radicand(radicand) {}
    
      bool operator < (const Number& rhs) const {
        // in case of equal numbers, i want smaller radicand first
        if (fabs(value - rhs.value) < 1e-12) return radicand < rhs.radicand;
        return value < rhs.value;
      }
    
      void print() const {
        if (value - (int)value < 1e-12) printf("%.0f", value);
        else printf("sqrt_%d(%d)",root_index, radicand); 
      }
    };
    
    std::vector<Number> numbers;
    double best_result = 1e100;
    Number best_numerator;
    Number best_denominator;
    
    double input;
    
    void compare_approximpation(const Number& numerator, const Number& denominator) {
       double value = numerator.value / denominator.value;
    
       if (fabs(value - input) < fabs(best_result - input)) {
          best_result = value;
          best_numerator = numerator;
          best_denominator = denominator;
       }
    }
    
    int main() {
    
      const int NUMBER_LIMIT = 100000;
      const int ROOT_LIMIT = 20;
    
      // only numbers created by this loops will be used
      // as numerator and denominator
      for(int i=1; i<=ROOT_LIMIT; i++) {
         for(int j=1; j<=NUMBER_LIMIT; j++) {
            double value = pow(j, 1.0 /i);
            numbers.push_back(Number(value, i, j));
         }
      }
    
      sort(numbers.begin(), numbers.end());
    
      scanf("%lf",&input); 
    
      int numerator_index = 0;
    
      for(int denominator_index=0; denominator_index<numbers.size(); denominator_index++) {
        // you were interested only in integral denominators
        if (numbers[denominator_index].root_index == 1) {
          // i use simple sweeping technique instead of binary search (its faster)
          while(numerator_index < numbers.size() && numbers[numerator_index].root_index &&
        numbers[numerator_index].value / numbers[denominator_index].value <= input) {
          numerator_index++;
          }
    
          // comparing approximations
          compare_approximpation(numbers[numerator_index], numbers[denominator_index]);
          if (numerator_index > 0) {
        compare_approximpation(numbers[numerator_index - 1], numbers[denominator_index]);
          }
        }
      }
    
      printf("Best approximation %.12lf = ", best_numerator.value / best_denominator.value);
      best_numerator.print();
      printf(" / ");
      best_denominator.print();
      printf("\n");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented the A* algorithm in AS3 and it works great except for
This is regarding AES algorithm. Suppose i have implemented a AES algorithm and encrypt
I have to implement a search algorithm for a school assignment. Right now, i'm
I have implemented the A* Algorithm According to Wikipedia's implementation at here However, it
I have implemented a Bellman-Ford algorithm to solve a problem (with a graph), but
I have implemented a basic Karplus-Strong algorithm. Ringbuffer, filling with white noise, output a
I have implemented a simple parallel merge sort algorithm in Java. This cuts the
I have implemented peterson' algorithm for mutual exclusion in shared memory. I am using
I have implemented a working quickSort algorithm using the first element in the array
I have implemented serial and parallel algorithm for solving linear systems using jacobi method.

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.