Announcement

Collapse
No announcement yet.

rounding floating point variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • rounding floating point variables

    i woiuld like to revist the rounding issues in powerbasic.
    i am using another basic for finance problems.
    but i need to use powerbasic now.
    i don't want to use integers or binary coded decimal variables for math.
    i read where the floating point variables use an IEEE STANDARD.
    i also read that rounding is not done correctly using these IEEE STANDARD.

    if i want to round a floating point variable of 2.5 to a whole number
    i want the answer of 3.
    i understand it will round to the nearest even number of 2 instead.

    also is there a built in function that will truncate a floating point
    to a whole number(chop off the numbers to the right of the decimal point)

    thanks paul




    [This message has been edited by paul d purvis (edited April 21, 2005).]
    p purvis

  • #2
    Paul --

    > i also read that rounding is not done correctly
    > using these IEEE STANDARD.

    That depends on what you mean by "correctly". PB does round numbers in the way that is specified by the IEEE standards. In other words it implements the IEEE standard correctly. But the IEEE standard may not be "correct" if you need another type of rounding.

    Check out the CEIL and INT functions in the documentation. If they are not exactly what you need, links in the Help File will point you toward additional functions.

    -- Eric


    ------------------
    Perfect Sync Development Tools
    Perfect Sync Web Site
    Contact Us: mailto:[email protected][email protected]</A>

    [This message has been edited by Eric Pearson (edited April 20, 2005).]
    "Not my circus, not my monkeys."

    Comment


    • #3
      >i don't want to use integers or binary coded decimal variables for math.

      Why the heck not? BCD (PB/DOS type FIX) is ideal for money calculations when rounding is undesireable.


      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        thanks for the posting and ideas
        my programming experience in basic (non powerbasic) has had on two types of
        numeric variables integers and real(floating), where there were only one type of each
        i am of some belief that if i can stick with just those two types of numeric variables, it
        might make my programming easier, and only use other types of numeric variables where i am
        force too.

        yes it will make my programs slower and bigger, but after converting some code to powerbasic lastnight.
        powerbasic was so much faster in math ability, it really did blow my socks off.
        that was really impressive, i guess it was using the math coprocessor.

        there is my code below
        i want to use extended floating point numbers
        integers are used just for flags and looping concerns for the most part

        in order to get values correct, rounding, etc,
        it seams i have to make sure i put the "##" behind all my values
        is that correct.


        and on the subject of ceil, int, fix
        do those return an integer and will those integers only be
        between -36768 to 36767
        or will they return larger values
        in the reference book it talks about those routines returning an integer or does
        it meant to mean those routines return a whole number.


        thanks for responses once again
        paul


        start of code

        rem program to compute the annual percentage rate on a loan( or rate of return)
        rem the term must be larger than 1 right now, that part is to be worked on
        rem odddays=represents the number of days odd days where there is more than a month until the firstpayment
        rem for example from january 1, 2000 to january 1,2000 there is a whole month and 0(zero) odd days
        rem for example from january 1, 2000 to january 2,2000 there is a whole month and 1 odd days
        rem for example from january 29, 2000 to february 29,2000 there is a whole month and 0 odd days
        rem for example from january 30, 2000 to february 29,2000 there is a whole month and 0 odd days
        rem for example from january 31, 2000 to february 29,2000 there is a whole month and 0 odd days
        rem for example from december 31, 1999 to february 29,2000 there is a whole month and 30 odd days
        rem odd days are worked from the first payment day back in equal months and a equal month is 30 days

        rem the payment schedule may have a first odd payment amount to the remaining regular payments


        rem ton##=top of note
        rem charges##=finance charges plus any other charges classified as interest for computing the annual percentage rate(apr)
        rem as defined by regulation z in usa by the federal reserve board.
        rem term%=the total payments on a loan
        rem firstpayment##= the first payment amount, amount maybe not equal to the remaining payments
        rem regpayment##= the remaining payment amounts
        rem

        rem start of program
        rem warning this program is not yet ready to do a calculation on the term being equal to 1
        rem and it is not complete, a term of 1 will return a value of -1

        TON##=1209.96##
        TERM%=36
        ODDDAYS%=0
        CHARGES##=209.96##
        FIRSTPAYMENT##=33.61##
        REGPAYMENT##=33.61##

        TEST%=0
        RATE##=0.0##
        COMPUTEBAL##=0.0##
        IF TERM%=1 THEN _
        IF ODDDAYS%=0 THEN RATE##=-1.0##:GOTO 1002


        ODDDAYS##=ODDDAYS%

        PRINCIPAL##=TON##-CHARGES##
        PRINCIPALTEMP##=INT(PRINCIPAL##*1000##)

        HIGHRATE##=20.0##
        LOWRATE##=0.0##

        99
        HIGHRATE##=HIGHRATE##+(HIGHRATE##*2.0##)
        COMPUTEBAL##=0.0##
        TEMP##=(HIGHRATE##/12.0)*.01##
        TEMP1##=1##+((ODDDAYS##/30.0##)*TEMP##)
        TEMP2##=1##+TEMP##
        COMPUTEBAL##=COMPUTEBAL##+(FIRSTPAYMENT##/(TEMP1##*(TEMP2##)^1))
        FOR J%=2 TO TERM%
        COMPUTEBAL##=COMPUTEBAL##+(REGPAYMENT##/ (TEMP1##*((TEMP2##)^J%)))
        NEXT J%
        IF COMPUTEBAL##<PRINCIPAL## THEN GOTO 999
        LOWRATE##=HIGHRATE##
        GOTO 99

        999
        100
        RATE##=((HIGHRATE##-LOWRATE##)/2.0##)+LOWRATE##

        101
        COMPUTEBAL##=0.0##

        TEMP##=(RATE##/12.0##)*.01##
        REM CALCULATING FOR A SINGLE ADVANCE AND SINGLE PAYMENT LESS THAN ONE YEAR
        IF TERM%=1 THEN _
        ODDDAYSINT##=0.0:_
        TEMP##=(RATE##*.01):_
        ODDDAYSINT##=((ODDDAYS##*TEMP##)/365)*PRINCIPAL##:_
        COMPUTEBAL##=TON##-ODDDAYSINT##:_
        GOTO 900

        TEMP1##=1##+((ODDDAYS##/30.0##)*TEMP##)
        TEMP2##=1##+TEMP##
        COMPUTEBAL##=COMPUTEBAL##+(FIRSTPAYMENT##/(TEMP1##*(TEMP2##)^1))
        FOR J##=2 TO TERM%
        COMPUTEBAL##=COMPUTEBAL##+(REGPAYMENT##/ (TEMP1##*((TEMP2##)^J##)))
        NEXT J##

        900
        IF TEST%>0 THEN _
        IF TEST%=4 THEN GOTO 1001 ELSE _
        IF TEST%=3 THEN GOTO 1000 ELSE _
        IF TEST%=2 THEN TEST%=3:GOTO 1000 ELSE _
        IF TEST%=1 THEN GOTO 997

        IF (HIGHRATE##-LOWRATE##)<.0001## THEN TEST%=1:GOTO 997
        IF PRINCIPALTEMP##=INT(COMPUTEBAL##*1000##) THEN _
        TEST%=1:GOTO 997
        IF PRINCIPAL##<COMPUTEBAL## THEN LOWRATE##=RATE##:GOTO 100
        IF PRINCIPAL##>COMPUTEBAL## THEN HIGHRATE##=RATE##:GOTO 100
        GOTO 100
        997
        TEST%=2
        GOTO 101
        1000
        IF COMPUTEBAL##<(PRINCIPAL##+.005##) THEN RATE##=RATE##-.0001##:GOTO 101
        LOWRATE##=RATE##+.0001##
        TEST%=4
        1001
        IF COMPUTEBAL##>(PRINCIPAL##-.005##) THEN RATE##=RATE##+.0001##:GOTO 101
        HIGHRATE##=RATE##-.0001##
        RATE##=LOWRATE##+((HIGHRATE##-LOWRATE##)/2##)
        1002
        PRINT LOWRATE##
        PRINT HIGHRATE##
        PRINT RATE##
        RATECALC##=ROUND(RATE##,4)
        PRINT RATECALC##
        STOP
        END

        end of code

        ------------------


        [This message has been edited by paul d purvis (edited April 21, 2005).]
        p purvis

        Comment


        • #5
          Paul,
          in order to get values correct, rounding, etc,
          it seams i have to make sure i put the "##" behind all my values
          is that correct.
          You can add ## after each variable or, if most of your variables are to be that type, you can add "DEFEXT A-Z" to the start of your code so all variables which aren't given a type will default to extended.

          But using extended precision variables doesn't solve the problem it just reduces the rounding error by 11bits (about 1/2000th) compared to double precision.

          <<do those return an integer and will those integers only be between -36768 to 36767>>

          They'll return any integer type, check the help file to see what types are available.

          Paul.

          ------------------

          Comment


          • #6
            >so all variables which aren't given a type will default to extended.

            While the variables may use either the "DEFxxx" default or the "LOCAL|STATIC|SHARED xxx AS TYPE" syntax to insure the specified precision, numeric literals must include a type specifier (as was done here).

            Still doesn't eliminate the rounding inherent in floating point operations; BCD is still IMO a better choice for all this 'money' stuff.




            [This message has been edited by Michael Mattias (edited April 21, 2005).]
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment

            Working...
            X