milkfilk
2007.04.23, 11:34 AM
If anyone could give me insight into a project they've worked on where they intentionally scaled values up to a base and then divide later if they want fractions. For example: you want to express "level finished is 50%". Fine.
double completed = 0.5;
But then you go and completed++ and unfortunately get an int back. Or maybe even:
completed = completed + 0.1;
But what I've found is a issue with precision that has finally hit my life outside of a textbook. What normally is a chapter that I skip over thinking its not applicable has landed square in my lap. Here's my test:
#include <iostream>
float addFloat(floatf, float i) {
return f + i;
}
int main(int argc, char* argv[])
{
while (f < 99.0) {
f = addFloat(f, 0.1);
std::cout << "Float: " << f << "\n";
}
return 0;
}
Eventually you'll see this.
Float: 21.7
Float: 21.8
Float: 21.9
Float: 22
Float: 22.1
Float: 22.2
Float: 22.3
Float: 22.4
Float: 22.5
Float: 22.6
Float: 22.7001
Float: 22.8001
Float: 22.9001
Float: 23.0001
Float: 23.1001
A similar thing happens with doubles. And some Unix's behave differently than OSX. And that's fine. It's been explained before. I'd simply multiply everything by 10, cast to int, divide by 10 back to float. Fine.
It's just, well, rgb values are nice as doubles. And I guess one has to plan ahead by how precise color will be then. If I wanted a hundreds place, then I'd have some constant int = 100 defined. So now I'm thinking "hmm, this could really be messy".
So while this might seem like a good post for a C++ newsgroup instead of this, my real question is: What stories can you tell me about this in your project? Do you accept this as norm? How far do you take your color precision or anything else? I'm sure it depends on the project but I would like to hear any tales anyway.
double completed = 0.5;
But then you go and completed++ and unfortunately get an int back. Or maybe even:
completed = completed + 0.1;
But what I've found is a issue with precision that has finally hit my life outside of a textbook. What normally is a chapter that I skip over thinking its not applicable has landed square in my lap. Here's my test:
#include <iostream>
float addFloat(floatf, float i) {
return f + i;
}
int main(int argc, char* argv[])
{
while (f < 99.0) {
f = addFloat(f, 0.1);
std::cout << "Float: " << f << "\n";
}
return 0;
}
Eventually you'll see this.
Float: 21.7
Float: 21.8
Float: 21.9
Float: 22
Float: 22.1
Float: 22.2
Float: 22.3
Float: 22.4
Float: 22.5
Float: 22.6
Float: 22.7001
Float: 22.8001
Float: 22.9001
Float: 23.0001
Float: 23.1001
A similar thing happens with doubles. And some Unix's behave differently than OSX. And that's fine. It's been explained before. I'd simply multiply everything by 10, cast to int, divide by 10 back to float. Fine.
It's just, well, rgb values are nice as doubles. And I guess one has to plan ahead by how precise color will be then. If I wanted a hundreds place, then I'd have some constant int = 100 defined. So now I'm thinking "hmm, this could really be messy".
So while this might seem like a good post for a C++ newsgroup instead of this, my real question is: What stories can you tell me about this in your project? Do you accept this as norm? How far do you take your color precision or anything else? I'm sure it depends on the project but I would like to hear any tales anyway.