Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Problem 3: Write a program that safely compares floating-point numbers with precision
- eps = 0.000001. Note that we cannot directly compare two floating-point numbers a and b by a==b
- because of the nature of the floating-point arithmetic. Therefore, we assume two numbers
- are equal if they are more closely to each other than a fixed constant eps. */
- using System;
- class ComparingFloats
- {
- static void Main()
- {
- float eps = 0.000001f;
- bool result = true;
- Console.WriteLine("Enter a number to compare");
- //reading the numbers from the console and taking their absolute value
- double firstNumber = Math.Abs(double.Parse(Console.ReadLine()));
- Console.WriteLine("Enter a number to compare");
- double secondNumber = Math.Abs(double.Parse(Console.ReadLine()));
- if (firstNumber>secondNumber)
- {
- result = firstNumber - secondNumber < eps;
- }
- else
- {
- result = secondNumber - firstNumber < eps;
- }
- Console.WriteLine(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement