If you are asking whether there is a floating point type that is not afflicted by these problems then the answer is no. It is a characteristic of the way floating point maths are done so you may get different results from different floating point libraries but some inaccuracy will always be there. The best you can do is to code around the small inaccuracies. It's really not as bad as it seems and I certainly wouldn't be put off using floats where they are necessary.Does fenix has any type like float(like real in Pascal), but more simply?
You can't. Any floating point operation is likely to get very small errors like this. Don't worry about it. That 0.000001 will not hurt you (unless you use == ).Sorry i must be stupid, but how i can do it? In this for example:
h1 and h2 - float
h1=2.38
h2=32
h1+h2=34.380001
In that case you should use a different strategy:No, it will hurt me! I make Calc 8)
It helps if you never actually do the 38/100 calculation. To actually achieve this you basically have to define each operation (+-*/) using only rationals. Some of these will be easier than others. Then when you read a user's input (e.g. 2.38) you parse it into a rational (2+38/100), do your calculations (2+38/100 * 2 -> 2*2+38*2/100 = 4 + 76/100) then print the result in th correct format (print integer part; print "."; print fractional part).You could use a rational type where 2.38 is represented as 2 and 38/100.
-----
But how it can help? It show this error again =(
I think you'll probably find that keeping track of the required level of accuracy is a lost easier than re-implementing everything to use a rational type.I will try it, but it will be more hard that i expect =) Thank you!
welcome to the field of computer science .Strange -_-
h1 and h2 - float
h1=2.38
h2=32
h1+h2=34.380001
Why? h1+h2 not 34.38 ?
Strange -_-
h1 and h2 - float
h1=2.38
h2=32
h1+h2=34.380001
Why? h1+h2 not 34.38 ?
float h1_float=2.38, h2_float=32;
int h1_int=h1_float; //0.38 gets lost
h2_int = h2_float; //0.000001 gets lost
//if there are no inaccuracies:
if ((h1_float - h1_int==0) && (h2_float - h2_int == 0) ){
//here comes your calculation
}
else
...
//here comes your code that uses integers to count instead of float variables