Direct casting possible?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1422
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Direct casting possible?

Post 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
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Direct casting possible?

Post 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.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Direct casting possible?

Post 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

Code: Select all

If ( A = 1.4) ; exact equality
You should have

Code: Select all

If ( ABS(A - 1.4) < Epsilon) ; close enough
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Direct casting possible?

Post 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

Code: Select all

If ( A = 1.4) ; exact equality
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?
bye,
Daniel
User avatar
Kukulkan
Addict
Addict
Posts: 1422
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Direct casting possible?

Post 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 :wink:

The approach of using such an Epsilon tolerance is quite interesting. :P Thank you for that!

Kukulkan
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Direct casting possible?

Post 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 :) )
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Direct casting possible?

Post 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 ;-) .
bye,
Daniel
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Direct casting possible?

Post 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. :)
Post Reply