Compare Floats with Epsilon

Compare Floats with Epsilon

Medium Python Data Types 15 views
Explanation Complexity

Problem Statement

Read two floats a and b. If |a-b| < 1e-9 output EQUAL. Else output GREATER if a>b else SMALLER.

Input Format

• First line: Two space-separated floats a b

Output Format

• Print one word:

• EQUAL if |a - b| < 1e-9

• GREATER if a > b

• SMALLER if a < b

Example

0.1 0.1000000005
EQUAL

Constraints

• -10^9 ≤ a, b ≤ 10^9

Concept Explanation

Input:
a = 1.000000001
b = 1.0

Difference = |a - b| = 0.000000001

Since it is less than 1e-9,
we treat them as equal.

Step-by-Step Explanation

1.Read floats a and b.

2.Compute diff = abs(a - b).

3.If diff < 1e-9, print "EQUAL".

4.Else if a > b, print "GREATER".

5.Otherwise, print "SMALLER".

Concept Explanation

Input:
a = 1.000000001
b = 1.0

Difference = |a - b| = 0.000000001

Since it is less than 1e-9,
we treat them as equal.

Step-by-Step Explanation

1.Read floats a and b.

2.Compute diff = abs(a - b).

3.If diff < 1e-9, print "EQUAL".

4.Else if a > b, print "GREATER".

5.Otherwise, print "SMALLER".

Input / Output Format

Input Format
• First line: Two space-separated floats a b
Output Format
• Print one word:

• EQUAL if |a - b| < 1e-9

• GREATER if a > b

• SMALLER if a < b
Constraints
• -10^9 ≤ a, b ≤ 10^9

Examples

Input:
0.1 0.1000000005
Output:
EQUAL

Example Solution (Public)

Python
import sysnp=sys.stdin.read().strip().split()nif not p: sys.exit(0)na=float(p[0]); b=float(p[1])nif abs(a-b)<1e-9:n  sys.stdout.write('EQUAL')nelif a>b:n  sys.stdout.write('GREATER')nelse:n  sys.stdout.write('SMALLER')

Official Solution Code

import sysnp=sys.stdin.read().strip().split()nif not p: sys.exit(0)na=float(p[0]); b=float(p[1])nif abs(a-b)<1e-9:n  sys.stdout.write('EQUAL')nelif a>b:n  sys.stdout.write('GREATER')nelse:n  sys.stdout.write('SMALLER')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.