Results 1 to 18 of 18

Thread: Doubles - Overflow

  1. #1

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Doubles - Overflow

    Does anybody know how not to get "Overflow" in the last line of this code??

    VB Code:
    1. Dim dLevel As Double
    2.     Dim dBitWiseAnd As Double
    3.  
    4.     'Just in case, the value of that field is rs!DAEGR_Level=2147483648
    5.     dBitWiseAnd = CDbl(rs!DAEGR_Level) And dLevel
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    probably bit-wise AND acts as an integer operation but you're using it on a floating point number.

  3. #3
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    I believe the And operator only works on integer types.
    I have two simple requests: 1) Use useful and specific topics. 2) Modify your topic to include [Resolved] when you problem has been resolved. Both of these make the bulletin boards more useful and efficient. Thanks.

  4. #4
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    What exactly are you doing? Bitwise operators aren't defined for floating-point numbers because the operation is meaningless. If you really want to use them on floating point numbers, use the CopyMemory API to convert them to longs, do the operation, and then convert mack to doubles. I don't understand why all the code is needed that DiGiTaIErRoR just provided (if any code is NOT self-documenting, that's a good example).
    I have two simple requests: 1) Use useful and specific topics. 2) Modify your topic to include [Resolved] when you problem has been resolved. Both of these make the bulletin boards more useful and efficient. Thanks.

  5. #5
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by VictorB212
    What exactly are you doing? Bitwise operators aren't defined for floating-point numbers because the operation is meaningless. If you really want to use them on floating point numbers, use the CopyMemory API to convert them to longs, do the operation, and then convert mack to doubles. I don't understand why all the code is needed that DiGiTaIErRoR just provided (if any code is NOT self-documenting, that's a good example).
    The bitwise operators are limited to the max signed value of the Long type.

    You wouldn't need to CopyMemory. You would just have to convert values over 2147483648 to it's respective negative signed Long value.

    So yeah... I'll remove my post.

  6. #6

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by DiGiTaIErRoR
    The And operator only works upto the max signed Long value.

    VB Code:
    1. Private Enum OpE
    2. opAnd = 1
    3. opOr = 2
    4. opXor = 3
    5. End Enum
    6.  
    7. Private Function dOps(ByVal Val1 As Double, ByVal Val2 As Double, ByVal OpCode As OpE) As Double
    8. Dim sVal1 As String, sVal2 As String
    9. sVal1 = B10To256(Val1, 6)
    10. sVal2 = B10To256(Val2, 6)
    11. For x = 1 To 6
    12. Select Case OpCode
    13. Case 1
    14. rs = rs & Chr$((Asc(Mid$(sVal1, x, 1)) And Asc(Mid$(sVal2, x, 1))))
    15. Case 2
    16. rs = rs & Chr$((Asc(Mid$(sVal1, x, 1)) Or Asc(Mid$(sVal2, x, 1))))
    17. Case 3
    18. rs = rs & Chr$((Asc(Mid$(sVal1, x, 1)) Xor Asc(Mid$(sVal2, x, 1))))
    19. End Select
    20. Next
    21. dOps = B256To10(rs)
    22. End Function
    23.  
    24. Private Function B10To256(ByVal initval As Double, ByVal Character_Return As Long) As String
    25. Dim rt As String
    26. Dim x As Long
    27. oi = initval
    28. If initval < 0 Then
    29. initval = 4294967296# + initval
    30. End If
    31. goal = initval
    32. DoEvents
    33. For x = Character_Return - 1 To 1 Step -1
    34. If goal >= (256 ^ x) Then
    35. rt = rt & Chr$(Int(goal / (256 ^ x)))
    36. goal = goal - (Int(goal / (256 ^ x)) * (256 ^ x))
    37. Else
    38. rt = rt & Chr$(0)
    39. End If
    40. Next
    41. rt = rt & Chr$(goal)
    42. B10To256 = rt
    43. End Function
    44.  
    45. Private Function B256To10(ByVal Base256Chrs As String) As Double
    46. Dim rt As Double
    47. Dim x As Long
    48. If Len(Base256Chrs) > 1 Then
    49. For x = Len(Base256Chrs) - 1 To 1 Step -1
    50. i = i + 1
    51. rt = rt + (Asc(Mid$(Base256Chrs, x, 1)) * (256 ^ i))
    52. Next
    53. End If
    54. B256To10 = rt + Asc(Right$(Base256Chrs, 1))
    55. End Function

    It's not fast enough for say, computing a CRC. But it should do just fine if not intensively used.
    VictorB212, that's exactly what I thought.

    Anyway, thanks so much for the code, DiGiTaIErRoR. I was thinking that I should do something like you did. Fortunately... you saved me a couple of coding-hours. Thanks a lot!!
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  7. #7
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by Mc Brain
    VictorB212, that's exactly what I thought.

    Anyway, thanks so much for the code, DiGiTaIErRoR. I was thinking that I should do something like you did. Fortunately... you saved me a couple of coding-hours. Thanks a lot!!
    Read my above post.

    And it'll save even more time.

  8. #8

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Anyway... I'm having problems with SQL now!!

    Code:
    UPDATE DAEUsers
    SET DAEUser_Level = DAEUser_Level - 4294967296 + 536870912
    WHERE (DAEUser_Level & 4294967296) > 0
    raises:
    Servidor: mensaje 403, nivel 16, estado 1, línea 1
    Invalid operator for data type. Operator equals boolean AND, type equals numeric.

    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  9. #9
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Like this:

    If val > 2147483647 then res = val - 4294967296

  10. #10

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I don't get what you mean.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  11. #11
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by Mc Brain
    I don't get what you mean.
    Just another method of using bitwise operators on 4 byte quanitities.

    if the number is > 2147483647 and < 4294967296 then subtract the number from 4294967296 and do the calculation, then add the 4294967296 back after.

    FFFFFFFF = -1 with a signed Long, unsigned it's 4294967295.

    Thus, 4294967295 - 4294967296 = -1.

    Unless VB supports unsigned longs....

  12. #12

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Ok, and how would you do a query like:

    UPDATE DAEUsers
    SET DAEUser_Level = DAEUser_Level - 2147483648
    WHERE (DAEUser_Level & 2147483648) > 0
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  13. #13
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by Mc Brain
    Ok, and how would you do a query like:

    UPDATE DAEUsers
    SET DAEUser_Level = DAEUser_Level - 2147483648
    WHERE (DAEUser_Level & 2147483648) > 0
    What's Daeuser_Level initially?

    2147483648 would be -2147483648 which is 80000000 and it's also 2147483648, but the sign messes it up.

    It's weird that the negative works, while the positive doesn't.

    Go figure.

  14. #14
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    This works:

    MsgBox -2147483648# And -2147483648#

    This overflows:

    MsgBox 2147483648# And 2147483648#

    almost makes you

  15. #15

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by DiGiTaIErRoR
    What's Daeuser_Level initially?

    2147483648 would be -2147483648 which is 80000000 and it's also 2147483648, but the sign messes it up.

    It's weird that the negative works, while the positive doesn't.

    Go figure.
    That's a problem! I don't know tha initial value of DAEUser_Level. I'm deleting a grant from the table, and since I'm freeing the User = 2 ^ x, I have to change all the UserLevel for all the users who had access to that action. This is why I don't know the initial value, because I want to change all the users' level (which had access to this level). Am I clear?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  16. #16
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    So do:

    VB Code:
    1. if DAEUser_Level > 2147483647 and DAEUser_Level < 4294967296 Then DAEUser_Level = 4294967296 - DAEUser_Level
    2.  
    3. dBitWiseAnd = -2147483648 And DAEUser_Level

  17. #17
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    This resolved?

  18. #18

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I don't know. I'll get at work in 2:30 hours time. Anyway, I'll change the way I'm doing things. Instead of doubles, I will have a table with all the permissions, a table with all the users, and a table for each the user_id and grant_id. If the user has a record in the latter that matches with the grant_id needed, he has access... if not, he hasn't.

    Thanks anyway for your help. But I'll guess this way should be much easier.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

Posting Permissions

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



Click Here to Expand Forum to Full Width