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
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  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
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    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 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    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 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  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
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: New in next thinBasic: numeric type casting

    Thanks Eros,

    I like it


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

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

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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