Advertisement
kuruku

ComparesFloat

Apr 16th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | None | 0 0
  1. using System;
  2.  
  3. //Write a program that safely compares floating-point numbers with precision eps = 0.000001. Note that we cannot
  4. //directly compare two floating-point numbers a and b by a==b because of the nature of the floating-point arithmetic.
  5. //Therefore, we assume two numbers are equal if they are more closely to each other than a fixed constant eps. Examples:
  6.  
  7. class ComparesFloat
  8. {
  9.     static void Main()
  10.     {
  11.         Console.Write("First number: ");
  12.         float firstNum = float.Parse(Console.ReadLine());
  13.         Console.Write("second number: ");
  14.         float secondNum = float.Parse(Console.ReadLine());
  15.  
  16.         bool isEqualNumbers = firstNum == secondNum;
  17.         Console.Write("? Equal : ");
  18.         Console.WriteLine(isEqualNumbers);
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement