English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

e.g: 234.567 & 234.569 are same upto 2 decimal places but,234.567 & 456.567 are not same.

2006-12-09 22:44:35 · 4 answers · asked by sweety 2 in Computers & Internet Programming & Design

4 answers

There is a function called "floor" im every programming language. It returns the biggest integer that is smaller than the float, ie. floor(234.567) = 234

So do domething like this (x and y are your floating numbers)

if floor(100*x) = floor(100*y) then they're same

2006-12-10 00:03:11 · answer #1 · answered by Daniel F 2 · 0 0

As you have not told the language you are working on so I'll just let you know a simple Idea how to do it.

convert both the values to characters (C++) or strings(JAVA). Char/String provides lots of functions to remove letters from it, once you have done that Convert it back into floating point. And there you are just do the simple check to find out if they are equal or not!

There are some other solutions also but I think this one is just one way to do it.

2006-12-09 23:19:43 · answer #2 · answered by farhan_sharif 1 · 0 0

What language are you working in?

Please post the code you have so far so we can help you correct it.
///

2006-12-09 23:10:44 · answer #3 · answered by jan 7 · 0 0

#ifndef FALSE     #define FALSE 0 #endif #ifndef TRUE     #define TRUE 1 #endif const double EPSILON = 0.01; // change this for more or less accuracy unsigned short sameNumber(double x, double y) {     if ((x - y) < EPSILON)         return TRUE;     else         return FALSE; }

2016-05-23 01:44:41 · answer #4 · answered by ? 4 · 0 0

fedest.com, questions and answers