|
-
July 7th, 2009, 11:49 AM
#16
Re: Debug works, Release doesn't?
Paul,
The log10(yTest) is 8 so that won't work. I tried the following and it too fails for the Release mode which i=30 instead of 10. Debug i=10. I suspect this is the same problem. Exact equality won't work in this case either.
Code:
#include <iostream>
using namespace std;
void main(){
int i;
double yTest;
yTest = 100000000.;
for(i=0;i<30;i++){
if(yTest-.01 == 0.) break;
yTest /= 10.;
}
cout<<i<<endl;
}
Regards,
Lowell
-
July 7th, 2009, 11:52 AM
#17
Re: Debug works, Release doesn't?
 Originally Posted by lspecht
Paul,
The log10(yTest) is 8 so that won't work.
Add 2 to that, since you are dividing by 10^(-2). I stated that log10 is a hint, not the exact answer. The number of times you loop in the first example is related to the number of digits in the number.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 7th, 2009 at 11:59 AM.
-
July 7th, 2009, 12:00 PM
#18
Re: Debug works, Release doesn't?
That would work for this example, however, I oversimplified my example. In my app, yTest can be any power of 10 and the .01 can be any power of 10 less than yTest. For the next case it could be yTest = 1000. and .000001. The problem is to determine the number of powers of 10 in the spread.
Regards,
Lowell
-
July 7th, 2009, 12:03 PM
#19
Re: Debug works, Release doesn't?
Sorry. I didn't read all of your last post. I need to think about it some more.
Regards,
Lowell
-
July 7th, 2009, 12:15 PM
#20
Re: Debug works, Release doesn't?
When using logs to determine number of digits, you have to be careful about boundary cases. I don't recall the exact rule offhand, though.
-
July 7th, 2009, 12:24 PM
#21
Re: Debug works, Release doesn't?
 Originally Posted by lspecht
That would work for this example, however, I oversimplified my example. In my app, yTest can be any power of 10 and the .01 can be any power of 10 less than yTest. For the next case it could be yTest = 1000. and .000001. The problem is to determine the number of powers of 10 in the spread.
Another possible solution is to normalize yTest as an integer (multiply it by 100, 1000, whatever). Then use that to figure out
1) How many times to loop.
2) The formula you use within the loop.
If you have 64-bit ints (you should), then maybe this is another approach you can take. Make yTest a 64-bit int, and see how that shakes out.
Regards,
Paul McKenzie
-
July 7th, 2009, 12:27 PM
#22
Re: Debug works, Release doesn't?
After thinking about your suggestion, Paul, I came up with this solution that should work. I'll have to add code to test for when y2Test is 1 or greater then subtract instead of add but that is straightforward. Note, I replaced the .01 with y2Test which I should have done in my original example since it is variable.
Thanks for all the help, everyone.
Code:
#include <iostream>
#include <math.h>
using namespace std;
void main(){
double i;
double yTest;
double y2Test;
yTest = 100000000.;
y2Test = .01;
i = abs(log10(yTest)) + abs(log10(y2Test));
cout<<i<<endl;
}
Regards,
Lowell
-
July 7th, 2009, 01:02 PM
#23
Re: Debug works, Release doesn't?
 Originally Posted by lspecht
After thinking about your suggestion, Paul,  I came up with this solution that should work. I'll have to add code to test for when y2Test is 1 or greater then subtract instead of add but that is straightforward. Note, I replaced the .01 with y2Test which I should have done in my original example since it is variable.
OK. But make sure you test on the different compilers. It still uses some floating point, but as long as "i" is the same then you should be OK.
I still think that normalizing everything to int, and doing the floating point calculations inside the loop is the way to go though.
Also, one more thing -- main() returns int, not void.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|