Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program that safely compares floating-point numbers with precision of 0.000001. Examples:(5.3 ; 6.01) false; (5.00000001 ; 5.00000003) true
- using System;
- class CompareFloatingPointNumbers
- {
- static void Main()
- {
- decimal a = 5.3m;
- decimal b = 6.01m;
- decimal a2 = 5.000000001m;
- decimal b2 = 5.00000003m;
- decimal precision = 0.000001m;
- bool equalAB = Math.Abs(a - b) < precision;
- Console.WriteLine("Are number {0} and {1} equal? {2}", a, b, equalAB);
- bool equalAB2 = Math.Abs(a2 - b2) < precision;
- Console.WriteLine("Are number {0} and {1} equal? {2}", a2, b2, equalAB2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement