Page 1 of 1
Direct casting possible?
Posted: Tue Mar 29, 2011 7:46 am
by Kukulkan
Hi,
I had a problem with floats, as it seems that PB automatically casts numbers to double. I managed like this:
This is not working correct:
Code: Select all
Variable.f = 1.4
If 1.4 > Variable.f
Debug "Test"
EndIf
This is working correct:
Code: Select all
Variable.f = 1.4
Comp1.f = 1.4
If Comp1.f > Variable.f
Debug "Test"
EndIf
Now, is there a direct way to cast a value? Something like this:
Code: Select all
If (float)1.4 > Comp1.f
Debug "Test"
EndIf
or this:
Code: Select all
If 1.4.f > Comp1.f
Debug "Test"
EndIf
Both is not working. Any idea how to cast values directly like in other languages?
PS. I know the problems about working and comparing floating point numbers. The only question is about a direct casting of values in source without storing them in variables before using.
Kukulkan
Re: Direct casting possible?
Posted: Tue Mar 29, 2011 7:55 am
by Shield
Unfortunately, there is no casting operator in PureBasic.
You probably have to write a Procedure for that task:
Code: Select all
Procedure.f DoubleToFloat(value.d)
ProcedureReturn value
EndProcedure
Implicit casting is, in my opinion, very tedious and led me into traps many times.
Re: Direct casting possible?
Posted: Wed Mar 30, 2011 5:36 pm
by buddymatkona
Checking for equality with floating point variables is considered bad form...especially if you expect to move data in and out of different hardware.
Instead of checking for exact equality, a safer approach is to define an EPSILON value just larger than the precision you lose by moving a long float into a short float.
So in other words where you might have
You should have
Code: Select all
If ( ABS(A - 1.4) < Epsilon) ; close enough
Re: Direct casting possible?
Posted: Wed Mar 30, 2011 6:58 pm
by DarkDragon
buddymatkona wrote:Checking for equality with floating point variables is considered bad form...especially if you expect to move data in and out of different hardware.
Instead of checking for exact equality, a safer approach is to define an EPSILON value just larger than the precision you lose by moving a long float into a short float.
So in other words where you might have
You should have
Code: Select all
If ( ABS(A - 1.4) < Epsilon) ; close enough
Where in the topic did you see someone checking floating point numbers for equality?
Re: Direct casting possible?
Posted: Thu Mar 31, 2011 6:30 am
by Kukulkan
Yes, I simply had to check if it was greater (or lesser) a given value. But this had been only an example for my question about implicit casting
The approach of using such an Epsilon tolerance is quite interesting.

Thank you for that!
Kukulkan
Re: Direct casting possible?
Posted: Thu Mar 31, 2011 2:55 pm
by buddymatkona
@DarkDragon
It is natural for people to expect equality between a hard coded FP value assigned to a variable vs the same value used in an expression but in practice they may differ by a little bit. (literally

)
Re: Direct casting possible?
Posted: Thu Mar 31, 2011 5:02 pm
by DarkDragon
buddymatkona wrote:@DarkDragon
It is natural for people to expect equality between a hard coded FP value assigned to a variable vs the same value used in an expression but in practice they may differ by a little bit. (literally

)
Yes I know, but this is off topic. Nobody in here compared them for equality

.
Re: Direct casting possible?
Posted: Thu Mar 31, 2011 11:39 pm
by buddymatkona
I see now I should have called it "A Solution For The FP Specific VALUE Comparison Problem" which might arise from incorrect use of the 1st two lines of code. Even if there was an equality check in the code , it would still be off topic but hey!...this is General Discussion.
