Announcement

Collapse
No announcement yet.

MOD returning double?

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

    MOD returning double?

    Have I missed sonething here? MOD should return an integer.
    Why do I get "15:30.33333333333334:20.0000000000000031" returned here ?

    #COMPILE EXE
    FUNCTION strhours(hournum AS DOUBLE) AS STRING
    'convert hours to "hh:mm:ss"
    FUNCTION = TRIM$(STR$(INT(hournum))) & ":" _
    & TRIM$(STR$((hournum * 60) MOD 60)) & ":" _
    & TRIM$(STR$((hournum * 3600) MOD 60))
    END FUNCTION

    FUNCTION numhours( strTime AS STRING) AS DOUBLE
    'convert "hh:mm:ss" to hours
    FUNCTION = VAL(PARSE$(strTime,":",1)) _
    + VAL(PARSE$(strTime,":",2))/60 _
    + VAL(PARSE$(strTime,":",3))/3600
    END FUNCTION

    FUNCTION PBMAIN
    LOCAL strTemp AS STRING
    strtemp = "15:30:20"
    MSGBOX strhours(numhours(strTemp))
    END FUNCTION


    ------------------
    Check out my free software at http://www.lexacorp.com.pg(all written in PB/DLL)

    [This message has been edited by Stuart McLachlan (edited June 16, 2001).]
    =========================
    https://camcopng.com
    =========================

    #2
    Switch the divisor sign around. Use / for floating point and \
    for integer.

    eg 3/5 = .6
    3\5 = 0

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

    Comment


      #3
      No, MOD will not necessarily return an integer ... it does an integer conversion *during* the MOD process, but the actual result is not certain to be an integer at all.

      MOD works something like this:
      Code:
      a! = 13! : b! = 2.7!
      c! = a! - FIX(a! / b!) * b! ' equivalent to c! = a! mod b!
      As you can see, MOD will return a floating point result.

      In your code, you may wish to add type specifiers to your numeric literals for clarity.

      However, the solution is to truncate the strings as necessary:
      Code:
      FUNCTION strhours(hournum AS DOUBLE) AS STRING
          'convert hours to "hh:mm:ss"
          FUNCTION = FORMAT$(FIX(hournum), "00") & ":" & _
                     FORMAT$((hournum * 60&) MOD 60&, "00") & ":" & _
                     FORMAT$((hournum * 3600&) MOD 60&, "00")
      END FUNCTION


      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


        #4
        [QUOTE]Originally posted by Lance Edmonds:
        [B]No, MOD will not necessarily return an integer ... it does an integer conversion *during* the MOD process, but the actual result is not certain to be an integer at all.

        As you can see, MOD will return a floating point result.

        In your code, you may wish to add type specifiers to your numeric literals for clarity.


        I've aready done the work round, but this is what the PB/DLL 6 help says:

        "The MOD operator divides two operands and returns the remainer of that division. Before the division, the operands are rounded to integers; the result of the division is also rounded to an integer before the remainder is calculated. The result of the MOD operation is always an (sic) whole number."

        I assumed that "an whole number" implied an integer. The float's being returned don;t appear to be whole numbers to me






        ------------------
        Check out my free software at http://www.lexacorp.com.pg(all written in PB/DLL)
        =========================
        https://camcopng.com
        =========================

        Comment


          #5
          Ah yes, but the ERRATA posting the FAQ forum has a more accurate description of the MOD operator.



          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎