Type Casting to all Types

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dummy
Enthusiast
Enthusiast
Posts: 162
Joined: Wed Jun 09, 2004 11:10 am
Location: Germany
Contact:

Type Casting to all Types

Post by Dummy »

Is it possible to add the commands Char(), Byte(), Word(), Long(), Quad(), Float(), Double() to do typecasting.
It would be nice if they'd be compiling time commands that only cause typecasting in that position(no jumps).

Most of all it would produce cleaner code if you have to cast:

Code: Select all

d.l = double(a.l) * double(b.) / double(c.l)
(I need doubles as a * b produces very big values and b / c or a / c produces very small values. Only the result is again in the range of a Long)

Atm that code looks like this:

Code: Select all

aDouble.d = a.l
d.l = aDouble.d * b.l * c.l
It doesn't look nice and it uses another var.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Maybe a single function named Cast() with flags like:

#PB_Double
#PB_Long
#PB_Char
etc...

Code: Select all

d.l = Cast(a.l,#PB_Double) * Cast(b.,#PB_Double) / Cast(c.l,#PB_Double)
Just a thought...
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

until it's implemented you can use

Code: Select all

Procedure double(x.d)
  ProcedureReturn x
EndProcedure
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

half a workaround :-)

upcasting is easy:

Code: Select all

castf.f = 0.0

whatever = castf + whatever
see here:

http://www.xs4all.nl/~bluez/datatalk/pu ... evaluation

downcasting is impossible, sorry, but if you pay good attention to the way pb maths work, you may not have to cast by changing the sequence of your expression
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

TypeCasting would be good.

Makes it obvious to the compiler as to what is intended.

Code: Select all

 .. AsDouble( flt1.f * flt2.f ) ..
-v-

Code: Select all

tmp1.d = flt1.f
tmp2.d = flt2.f
.. (tmp1 * tmp2) ..
Can also make for better readability.

May trim code size and clocks a little?

And as a side benefit, will also force some brackets that help compiler and programmer with understanding/enforcing order of priority in expression evaluation.

But mainly, the coder controls what is happening in the code re types.
@}--`--,-- A rose by any other name ..
Dummy
Enthusiast
Enthusiast
Posts: 162
Joined: Wed Jun 09, 2004 11:10 am
Location: Germany
Contact:

Post by Dummy »

Dare2 wrote:TypeCasting would be good.

Makes it obvious to the compiler as to what is intended.

Code: Select all

 .. AsDouble( flt1.f * flt2.f ) ..
-v-

Code: Select all

tmp1.d = flt1.f
tmp2.d = flt2.f
.. (tmp1 * tmp2) ..
Can also make for better readability.

May trim code size and clocks a little?

And as a side benefit, will also force some brackets that help compiler and programmer with understanding/enforcing order of priority in expression evaluation.

But mainly, the coder controls what is happening in the code re types.
That's exactly what I mean :)

Hopefully Fred will add it.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

dare & dummy, what's the problem?

code is IMPLICITLY cast down depending on how you define the destination variable

a.b = whatever

is casted down as a byte, etc. etc. etc.

and if you want to go up and down...

z.f = 0.0
a.b = z.f + whatever

(although in the last form i'd like a keyword, yes, but ok, can't have all)

what am i missing?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Implicit is fine, and still needed. Explicit is an additional wish, and can be useful/nice. To force the issue, for example.

z.f = 0.0
a.b = z.f + whatever

Does the job. So would a typecaster, without the additional assignment.

No biggie, this is the wishlist thread, not the bugs report. Or will PureBasic be cast in concrete as at v 4.00 and never move on? :P :)
@}--`--,-- A rose by any other name ..
Post Reply