Announcement

Collapse
No announcement yet.

Floating point constants?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
    Ken Elvehjem
    Member

  • Ken Elvehjem
    replied
    OK

    Thanks for the responses and esp. the notes on text substitution... As ever there's more than one way to do it!

    With appreciation,

    Ken

    Leave a comment:

  • Michael Mattias
    Member

  • Michael Mattias
    replied
    I am working on that invoice printing application and wanted all the X-Y locations for literals, data and images in constants, in inches. So I just multiplied by 100 to make the constant, and divide by 100 in line as needed.

    e.g.,
    Code:
    %TWO_INCHES = 200&
    %ONE_HALF_INCH = 50&
    Works in this application just ducky.

    Leave a comment:

  • John Gleason
    Member

  • John Gleason
    replied
    MACRO mConst2 = (315 / 1000)
    Yes, good point Petr. I too would prefer the "MACRO mConst2 = 0.315##" but with the glitch noted in previous posts where it gets assigned as a DOUBLE rather than EXT, the (315 / 1000) looks like the ticket for MACRO full EXT precision.

    Leave a comment:

  • Petr Schreiber jr
    Member

  • Petr Schreiber jr
    replied
    Hi,

    in PB I prefer to use MACROs for floating point constatns.
    To maintain precision, I add the "##" for EXT.
    Code:
    MACRO mConst2 = 0.315##
    But the 315/1000 way is very nice , I would just suggest to surround it with brackets:
    Code:
    MACRO mConst2 = (315 / 1000)
    Because you could get into trouble - MACRO works like text substitution, which without brackets leads to "surprises" in case you do not pay much attention when writing code. See here:

    Code:
    #COMPILE EXE
    #DIM ALL
    MACRO mConst         = 315 / 1000
    MACRO mConstBrackets =(315 / 1000)
    FUNCTION PBMAIN () AS LONG
    
           ? STR$(mConst^2)
           ' = 0.000315 because it is evaluated like 315 / (1000 ^ 2) thanks to operator precedence
           ? STR$(mConstBrackets^2)
           ' = 0.099225 because it is correctly evaluated like (315/1000)^2
           
    '       WAITKEY$
    
    END FUNCTION
    Petr
    Petr Schreiber jr
    Member
    Last edited by Petr Schreiber jr; 28 Jul 2008, 03:09 PM.

    Leave a comment:

  • John Gleason
    Member

  • John Gleason
    replied
    Note below code demonstrating precision differences. It may have relevance.
    Code:
    #COMPILE EXE
    #DIM ALL
    MACRO mConst1 = .315       '<< okay if single precision is adequate
    MACRO mConst2 = 315 / 1000 '<< if you need it to be EXACTLY .315
    FUNCTION PBMAIN () AS LONG
           
           'thanks to Paul Purvis, I know 315 / 1000 = VAL(".315") which is the
           'exact EXT representation of .315 (or whatever your float pt. value is)
           'The following shows as defined, it is a single.
           ? mConst1 - (315 / 1000)
           ? mConst2 - (315 / 1000)
    '       WAITKEY$
    
    END FUNCTION

    Leave a comment:

  • Ken Elvehjem
    Member

  • Ken Elvehjem
    replied
    OK,

    Thanks for the responses, guys. I was wondering if I'd had a dumb-out or something. Isn't it a little odd that floating point constants aren't "built-in" considering the number of physical constants and bugger factors involved in science and industry???

    On the other hand, I haven't missed them in the last 10 years... guess I'll bone up on macros.

    Thanks again for the replies!

    Ken

    Leave a comment:

  • StanHelton
    Member

  • StanHelton
    replied
    MACRO My_Constant = 33.7

    Leave a comment:

  • Walt Thompson
    Member

  • Walt Thompson
    replied
    Floating Point Constants

    Hi Ken;

    There are two ways to solve this:
    1. Declare a numeric constant (%337) and divide it by 10 when ever you use it.

    2. Easier, declare a string constant ($33.7) and convert it to numeric whenever you use it. Example: X = VAL($33.7) where X is previously DIM'd as SINGLE.

    Leave a comment:

  • Eric Pearson
    Member

  • Eric Pearson
    replied
    > using a % sign is apparently indicating an integer

    Correct.

    The easiest way to name a floating point value is to use a MACRO.

    -- Eric

    Leave a comment:

  • Ken Elvehjem
    Member

  • Ken Elvehjem
    started a topic Floating point constants?

    Floating point constants?

    Hi,

    I am trying to create a global constant for a floating point value.

    I can find examples galore of Integer constants in the help files, but no references to using a floating point constant.

    using a % sign is apparently indicating an integer... so this doesn't work:

    %My_Constant = 33.7

    I know I could just create a global variable and assign a value to it... Is that what I have to do, or is there something I have missed?

    Ken
Working...
X