comparing reducible fractions using float/double division

mechatroner

Let's say I have two fractions: a/b and c/d, where a,b,c,d are all integers greater than 0. Is it safe to check their equality using the following function?:

bool are_equal_fractions(int a, int b, int c, int d) {  
   return (static_cast<double>(a) / b == static_cast<double>(c) / d);
}

According to another question: can I compare two fractions if both have denominator with power of 2 I can use this method when both denominators are powers of 2, but what about more generic case?

Patricia Shanahan

Although every int can be represented as a double, many int ratios cannot be represented exactly, and very similar but slightly different fractions may round to the same double.

Consider a=2147483647, b=2147483646, c=2147483646, d=2147483645. The denominator of 2147483646/2147483645 will be a multiple of 5, even in its lowest terms. The denominator of 2147483647/2147483646 is not a multiple of 5, so they are not equal.

cout << are_equal_fractions(2147483647, 2147483646, 2147483646, 
        2147483645) << endl; 

outputs "1".

In general, equal fractions in this pattern would mean:

(i+2)/(i+1) == (i+1)/i
i*(i+2) == (i+1)*(i+1)
i^2 + 2*i == i^2 + 2*i + 1
0 == 1

which has no solution.

The smallest counter-example following this pattern is are_equal_fractions(67114658,67114657,67114657,67114656). I do not think any other pattern could have a closer non-equal ratio, so it is probably safe for values less than this case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Comparing fractions using pointers program in C efficient method

From Dev

Comparing fractions with struct

From Dev

Comparing Fractions with Bool

From Dev

Comparing doubles' fractions

From Dev

Comparing fractions and decimal approximations in Matlab

From Dev

Simplify fractions at compile time to avoid division by zero

From Dev

Modular arithmetic using fractions

From Dev

Calculator for fractions using classes

From Dev

Year fractions using Actual/365 convention in R

From Dev

Subtraction of binary fractions using 2's complement

From Dev

Changing printing format for fractions using sympy and PythonTex

From Dev

Changing printing format for fractions using sympy and PythonTex

From Dev

Using php's explode function on fractions

From Dev

how to reducible this SQL query

From Dev

Division in awk using variables

From Dev

using division in awk script

From Dev

Calculator division using javascript

From Dev

Comparing Faces using landmarks?

From Dev

Comparing values using hashtable

From Dev

Comparing directories using diff

From Dev

Comparing Strings using a buffer

From Dev

Create compact/human-friendly floats using unicode vulgar fractions

From Dev

calculating volume using fractions (decimals) and getting strange results for filler program

From Dev

using division operator (/) on strings in javascript

From Dev

Integer division without using the / or * operator

From Dev

division string using regular expressions

From Dev

Pandas group by aggregate using division

From Dev

division string using regular expressions

From Dev

Division using select statement in mysql