Real Type


XaMMaX90

Member
Joined
Jun 6, 2006
Messages
216
I try
Code:
test real;
and
real test;
but doesn't work =(

P.S.
Sorry for my english =)
 
Strange -_-
h1 and h2 - float
h1=2.38
h2=32
h1+h2=34.380001
Why? h1+h2 not 34.38 ?
 
Try this:

h1 and h2 - float
h1=2.38
h2=32.0
h1+h2=???

Just a thought, haven't tried it.

-Kensupen
 
Floating poing maths is very likely to introduce tiny errors like this. It's just the way it is; rounding errors and other sources of inaccuracy all add up. I know it seems like it should be perfectly accurate on such a simple calculation but that's just not how it works. As an asside, when you are testing for equality in floats you should always allow a small margin of error i.e. instead of asking, (float1 == float2) you should ask ((float1 + margin >= float2) && (float1 - margin <= float2)). For me this is yet another reason that floats are really annoying and why not to use them where possible.

C
 
Does fenix has any type like float(like real in Pascal), but more simply?
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.
I'm just saying not to be freaked out by the small inaccuracies because they are insignificant and to be aware of them when testing for equality of floats. I've be bitten by the equality of float thing in the past and it took me ages to figure it out so I'm just spreading the knowledge because it annoyed me so much at the time.

C
 
Last edited by a moderator:
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
 
No, it will hurt me! I make Calc 8)
In that case you should use a different strategy:
You can use double floats (sorry I don't know fenix) but then you are likely to get errors that are 0.0000000000001.

You can keep track of the accuracy of the input and throw the rest away.
i.e.
32 has 0 decimal places
2.38 has 2 decimal places
therefore 32+2.38 must have only 2 decimal places.
(complication: 0.1 * 0.03 = 0.003 whici has 3 decimal places but 0.1 + 0.03 = 0.13 which has only 2 d.p.)

You could use a rational type where 2.38 is represented as 2 and 38/100.

Making a calculator that has an appropriate level of accuracy is not quite as simple as you might think. I'm sure it will be an interesing project though!

C
 
Last edited by a moderator:
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 =(

32 has 0 decimal places
2.38 has 2 decimal places
therefore 32+2.38 must have only 2 decimal places.
(complication: 0.1 * 0.03 = 0.003 whici has 3 decimal places but 0.1 + 0.03 = 0.13 which has only 2 d.p.)
-----
Hm... it can help!
 
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 =(
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).
It is certainly not a simple approach but it can give you *really* good accuracy.

I'm glad you like the other one though.

C
 
Last edited by a moderator:
Strange -_-
h1 and h2 - float
h1=2.38
h2=32
h1+h2=34.380001
Why? h1+h2 not 34.38 ?
welcome to the field of computer science .

this is one of the basics of computer science. for us it's normal to work in base 10, but never ever forget that the computers doesn't work in base 10, it works in base 2

nice round decimal numbers will rarely be nice round binary numbers. so your pc will take the nearest number, but as you notice there'll be a slight difference.
 
Last edited by a moderator:
Strange -_-
h1 and h2 - float
h1=2.38
h2=32
h1+h2=34.380001
Why? h1+h2 not 34.38 ?

how about this?

Code:
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
 
Last edited by a moderator:
Back
Top