Page 1 of 1
Type Casting to all Types
Posted: Sat Feb 18, 2006 3:35 pm
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.
Posted: Sat Feb 18, 2006 7:05 pm
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...
Posted: Sat Feb 18, 2006 7:46 pm
by jack
until it's implemented you can use
Code: Select all
Procedure double(x.d)
ProcedureReturn x
EndProcedure
Posted: Sat Feb 18, 2006 8:30 pm
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
Posted: Sun Feb 19, 2006 1:35 am
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.
Posted: Sun Feb 19, 2006 3:40 pm
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.
Posted: Sun Feb 19, 2006 4:37 pm
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?
Posted: Sun Feb 19, 2006 4:56 pm
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?
