| | |
problem using floor function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
i am writing a function to convert fractions to intgers for example:
.636 -> 636 or .45 -> 45
for some inputs it is working fine for example:

But for some inputs :

which i cant understand the floor and the input are the same so why the function does not return
some inputs also do this:

floor(23450) = 23499
weird?
if anybody has another approach (another method) to do the job i would be thankful
.636 -> 636 or .45 -> 45
C++ Syntax (Toggle Plain Text)
int ret_int(double input) { while(true) { // this line is for debugging only cout << input << " " << floor(input) << endl; if(input == floor(input)) return input; input = input * 10; } }
for some inputs it is working fine for example:
But for some inputs :
which i cant understand the floor and the input are the same so why the function does not return
some inputs also do this:
floor(23450) = 23499
weird?
if anybody has another approach (another method) to do the job i would be thankful
-1
#2 Oct 16th, 2009
I had very similar output in one of my project, it seems I declared a double somewhere, but didn't instantiate it...
I don't know if this is applicable to you, but now that I've instantiated it, it seems to be better...
cpp Syntax (Toggle Plain Text)
double somedouble;
cpp Syntax (Toggle Plain Text)
double somedouble = 0.0;
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 6
1
#4 Oct 16th, 2009
Remember: 2.0+2.0!=4.0. I think this is just roundoff error, .245*1000 can be smaller then 245.
edit: Solution: define new class for double and make new operator== which will do something like that:
where epsilon is desired precision (10**-10 would be good).
edit: Solution: define new class for double and make new operator== which will do something like that:
C++ Syntax (Toggle Plain Text)
if(a<b*(1+epsilon) && a>b*(1-epsilon)) return true; return fals;
Last edited by Zjarek; Oct 16th, 2009 at 6:53 pm.
0
#6 Oct 17th, 2009
•
•
•
•
constructing a whole class to solve such a problem!
is nt there any simpler solutions
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#7 Oct 23rd, 2009
ok i did what the website told me to do , writing a function to check if two double numbers are equal or not , this solved the first problem when the input is .23 for eample (see the second picture in my first post)
but it did not solve the second problem (see the third picture in my first post) : floor(2345) = 2344
this is a problem with the floor function .
any suggestions !
but it did not solve the second problem (see the third picture in my first post) : floor(2345) = 2344
this is a problem with the floor function .
any suggestions !
![]() |
Similar Threads
- Problem with random function (C++)
- Recursive Function Problem (C++)
- Problem with string to function argument (C++)
- Problem wiht my function (PHP)
- Problem on calling a function (C++)
- problem with calling a function (C++)
- problem in sqlcxt() function in pro*c (C++)
- problem with system() function... (C++)
- Function Problem. Please help (PHP)
Other Threads in the C++ Forum
- Previous Thread: error in evc++
- Next Thread: Visual C++ OPC client
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






