PDA

View Full Version : New in next thinBasic: numeric type casting



ErosOlmi
16-07-2008, 22:42
In some circumstances it is necessary to execute numeric expressions and cast result to a specific numeric data type: INTEGER, LONG, DOUBLE, ...
In some special case it is even necessary to CAST intermediate results inside a more complex numeric expression.

So far it was not easy other than writing some obscure code like:


'---CZ is a DOUBLE
cvd(mkd$(cz - 0.6))
'---Convert a number to a string buffer representing a binary DOUBLE and than get back to a double number



or defining a variable of the needed type assigning the result of intermediate expressions, than using that variable to pass value to a function that wants a DOUBLE or use it into more complex expression.

From the next version you will have more options: numeric data casting.

For example, every single math expression can be casted to a numeric type using the syntax: (math expression) AS NumericType
So to return a DOUBLE from a math expression you can type something:


... (MyDouble - 0.6) AS DOUBLE
... (0.123) AS DOUBLE


or


MyEntityID = tbgl_entityfindbypos( %SCENE1, %TBGL_BOX, cx, cy, (cz - 0.6) as double )


or even casting intermediate expressions as many times as needed (following example exagerated by choice in order to give you the idea):


... (MyExt * (MyDouble + 1.12345) AS DOUBLE * (10 + .598) AS DOUBLE) AS single


The above syntax will be valid in any place where a numeric expression is needed, will not influence current scripts, will not slow down execution (it is just an IF more in compiled code) and will be optional at any level.

Hope you like it.
Regards
Eros

_________________________________________________
Attached thinCore.dll. Substitute the one you have into your \thinBasic\ directory
Important: attached file is just for testing. You need to have current latest official thinBasic installed in order to test attached Core.

Updates:

2008.07.17: all numeric assignments are now making internal rounding depending on receiving variable type.
2008.07.18: also numeric expressions passed to API or, in general, external DLLs functions are now numeric rounded to the numeric type indicated in function prototype (DECLARE statement).
Fixed a bug in #IF/#ENDIF preparsing statement introduced into previous attach.

GSAC3
16-07-2008, 22:49
Eros:

Thanks -- That's a winner!

Don

ErosOlmi
16-07-2008, 23:13
Forgot to attach a working thinCore.dll
If someone wants to test it now it is possible.

Petr Schreiber
17-07-2008, 10:57
Thanks Eros,

this is very clean way to handle precision :)


Thanks,
Petr

ErosOlmi
17-07-2008, 13:14
Update thinCore.dll attached to first post of this thread.
_________________________________________________

All numeric assignments and data retrieving will now internally automatically cast (round) numeric expressions depending on receiving or source numeric variable type.

Regards
Eros

Petr Schreiber
17-07-2008, 14:03
Maxxxxximum coding comfort,

thanks a lot Eros, really amazing and quite unique even across different languages.


Petr

GSAC3
17-07-2008, 16:34
Eros:

Things keep getting better and better for us number-crunchers.

The change you made today makes thinBasic function like FORTRAN in this regard; and that's great.

Thanks.

Don

ErosOlmi
17-07-2008, 16:57
I'm happy you are happy ;)

Anyhow, please report any inconvenience, bug or other un-expected behaves.
I did a global check and should have not forgot any place where rounding should take effect but, you know, I'm only human.

I know that remains two places where I need to work on:

when passing numeric expressions to keywords present in external modules (I need to work on this with people developing modules)
when passing numeric expressions to external DLL functions (here I think I can easily fix it by next thinBasic version)


Ciao
Eros

ErosOlmi
18-07-2008, 06:58
Update thinCore.dll attached to first post of this thread.
_________________________________________________

Also numeric expressions passed to API or, in general, external DLLs functions are now numeric rounded to the numeric type indicated in function prototype (DECLARE statement).
Fixed a bug in #IF/#ENDIF preparsing statement introduced into previous attached thinCore.

Regards
Eros

Petr Schreiber
18-07-2008, 21:11
Thanks Eros,

I like it :)


Petr