Results 1 to 10 of 10

Thread: New in next thinBasic: numeric type casting

  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,302
    Blog Entries
    2
    Rep Power
    10

    New in next thinBasic: numeric type casting

    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:
    [code=thinbasic]
    '---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


    [/code]
    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:
    [code=thinbasic]
    ... (MyDouble - 0.6) AS DOUBLE
    ... (0.123) AS DOUBLE
    [/code]

    or
    [code=thinbasic]
    MyEntityID = tbgl_entityfindbypos( %SCENE1, %TBGL_BOX, cx, cy, (cz - 0.6) as double )
    [/code]

    or even casting intermediate expressions as many times as needed (following example exagerated by choice in order to give you the idea):
    [code=thinbasic]
    ... (MyExt * (MyDouble + 1.12345) AS DOUBLE * (10 + .59 AS DOUBLE) AS single
    [/code]

    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.

    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  2. #2

    Re: New in next thinBasic: numeric type casting

    Eros:

    Thanks -- That's a winner!

    Don
    XPS 1710

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,302
    Blog Entries
    2
    Rep Power
    10

    Re: New in next thinBasic: numeric type casting

    Forgot to attach a working thinCore.dll
    If someone wants to test it now it is possible.

    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,816
    Blog Entries
    3
    Rep Power
    695

    Re: New in next thinBasic: numeric type casting

    Thanks Eros,

    this is very clean way to handle precision


    Thanks,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,302
    Blog Entries
    2
    Rep Power
    10

    Re: New in next thinBasic: numeric type casting

    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
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,816
    Blog Entries
    3
    Rep Power
    695

    Re: New in next thinBasic: numeric type casting

    Maxxxxximum coding comfort,

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


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  7. #7

    Re: New in next thinBasic: numeric type casting

    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
    XPS 1710

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,302
    Blog Entries
    2
    Rep Power
    10

    Re: New in next thinBasic: numeric type casting

    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
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  9. #9
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,302
    Blog Entries
    2
    Rep Power
    10

    Re: New in next thinBasic: numeric type casting

    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
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,816
    Blog Entries
    3
    Rep Power
    695

    Re: New in next thinBasic: numeric type casting

    Thanks Eros,

    I like it


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

Similar Threads

  1. Next thinBasic: how numeric tokens are stored internally
    By ErosOlmi in forum thinBasic vaporware
    Replies: 14
    Last Post: 12-07-2008, 16:47

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •