944,156 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 799
  • C++ RSS
Oct 16th, 2009
0

problem using floor function

Expand Post »
i am writing a function to convert fractions to intgers for example:
.636 -> 636 or .45 -> 45

C++ Syntax (Toggle Plain Text)
  1. int ret_int(double input)
  2. {
  3. while(true)
  4. {
  5. // this line is for debugging only
  6. cout << input << " " << floor(input) << endl;
  7.  
  8. if(input == floor(input)) return input;
  9. input = input * 10;
  10. }
  11. }

for some inputs it is working fine for example:

Click image for larger version

Name:	1.jpg
Views:	27
Size:	20.4 KB
ID:	12148



But for some inputs :
Click image for larger version

Name:	2.jpg
Views:	27
Size:	29.6 KB
ID:	12149


which i cant understand the floor and the input are the same so why the function does not return

some inputs also do this:

Click image for larger version

Name:	3.jpg
Views:	134
Size:	25.5 KB
ID:	12150


floor(23450) = 23499

weird?
if anybody has another approach (another method) to do the job i would be thankful
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I_Empire is offline Offline
10 posts
since Sep 2009
Oct 16th, 2009
-1
Re: problem using floor function
I had very similar output in one of my project, it seems I declared a double somewhere, but didn't instantiate it...
cpp Syntax (Toggle Plain Text)
  1. double somedouble;
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)
  1. double somedouble = 0.0;
Reputation Points: 32
Solved Threads: 13
Junior Poster in Training
ryuslash is offline Offline
57 posts
since Jul 2009
Oct 16th, 2009
0
Re: problem using floor function
No there is nothing to initialize
and even if why?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I_Empire is offline Offline
10 posts
since Sep 2009
Oct 16th, 2009
1
Re: problem using floor function
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:
C++ Syntax (Toggle Plain Text)
  1. if(a<b*(1+epsilon) && a>b*(1-epsilon)) return true;
  2. return fals;
where epsilon is desired precision (10**-10 would be good).
Last edited by Zjarek; Oct 16th, 2009 at 6:53 pm.
Reputation Points: 20
Solved Threads: 18
Junior Poster in Training
Zjarek is offline Offline
79 posts
since Oct 2009
Oct 17th, 2009
0
Re: problem using floor function
constructing a whole class to solve such a problem!
is nt there any simpler solutions
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I_Empire is offline Offline
10 posts
since Sep 2009
Oct 17th, 2009
0
Re: problem using floor function
Click to Expand / Collapse  Quote originally posted by I_Empire ...
constructing a whole class to solve such a problem!
is nt there any simpler solutions
http://www.parashift.com/c++-faq-lit...html#faq-29.17
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 23rd, 2009
0
Re: problem using floor function
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 !
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I_Empire is offline Offline
10 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error in evc++
Next Thread in C++ Forum Timeline: Visual C++ OPC client





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC