Results 1 to 18 of 18

Thread: * RESOLVED * How to declare the maximum value of a Double as a const

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    * RESOLVED * How to declare the maximum value of a Double as a const

    This code gives me an error. I'm not sure how to declare a const with an exponent. Does anyone know how to make this declaration? I want the maximum value that a double can hold as a constant for validation purposes.

    VB Code:
    1. public const dblMax as Double=1.79769313486232E308

    I also need the smallest negative value. This doesn't give me an error, but I'm not sure that it's not subtracting 308 from the value which would make it incorrect.

    VB Code:
    1. Public Const dblMin As Double = 4.94065645841247E-324
    Last edited by cafeenman; May 28th, 2002 at 05:28 PM.

  2. #2
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Do you really need the absolute maximum and minimum?

    I'm just curious, why would the user need to be able to enter in values in that entire range?

  3. #3

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I just want to be able to validate user input before VB does so I can return a real error message instead of the default error that VB would raise.

    I don't think it will be a problem for a double data type, but even so, I'd like to know how to do it.

  4. #4

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    basically, this is what I've got. The remmed out lines I haven't figured out how to make work yet.
    VB Code:
    1. Public Const MIN_BYTE As Byte = 0
    2. Public Const MAX_BYTE As Byte = 255
    3. Public Const MIN_CURRENCY As Currency = -922337203685477#
    4. Public Const MAX_CURRENCY As Currency = 922337203685477#
    5. 'Public Const MIN_DOUBLE As Double = 4.94065645841247E-324
    6. 'public const MAX_DOUBLE as Double=1.79769313486232E+308
    7. Public Const MIN_INTEGER As Integer = -32768
    8. Public Const MAX_INTEGER As Integer = 32767
    9. Public Const MIN_LONG As Long = -2147483648#
    10. Public Const MAX_LONG As Long = 2147483647
    11. 'public const MIN_SINGLE as Single=same problem as double - but smaller value
    12. 'public const MAX_SINGLE as Single = Ditto

  5. #5

  6. #6

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    thanks. It likes that and therefore, so do I. Much appreciated.

    So for the MIN_DOUBLE I'm declaring it not as a negative number, but as a negative exponent?

    I'm confused now. That would mean a double can only be greater than 0? What am I missing here?

  7. #7

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    OK, the problem is that there are actually 4 limits to a single and a double so I can't just declare a min and max value. I need a min and max negative value and a min and max positive value.

    Valid numbers are from -1.79769313486231E308 to -4.94065645841247E-324 for negative values;

    4.94065645841247E-324 to 1.79769313486232E308 for positive values.

    Also, what John said appears to work, so thank you for that information.

  8. #8
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    just curious... How do you know what the max value is??

    you write a loop and print the value to a file until and error occurs?

  9. #9

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    From MSDN:

    Visual Basic for Applications Reference

    Data Type Summary


    The following table shows the supported data types, including storage sizes and ranges.

    Data type Storage size Range
    Byte 1 byte 0 to 255
    Boolean 2 bytes True or False
    Integer 2 bytes -32,768 to 32,767
    Long
    (long integer) 4 bytes -2,147,483,648 to 2,147,483,647
    Single
    (single-precision floating-point) 4 bytes -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values
    Double
    (double-precision floating-point) 8 bytes -1.79769313486231E308 to
    -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values
    Currency
    (scaled integer) 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
    Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;
    +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is
    +/-0.0000000000000000000000000001
    Date 8 bytes January 1, 100 to December 31, 9999
    Object 4 bytes Any Object reference
    String
    (variable-length) 10 bytes + string length 0 to approximately 2 billion
    String
    (fixed-length) Length of string 1 to approximately 65,400
    Variant
    (with numbers) 16 bytes Any numeric value up to the range of a Double
    Variant
    (with characters) 22 bytes + string length Same range as for variable-length String
    User-defined
    (using Type) Number required by elements The range of each element is the same as the range of its data type.


    Note Arrays of any data type require 20 bytes of memory plus 4 bytes for each array dimension plus the number of bytes occupied by the data itself. The memory occupied by the data can be calculated by multiplying the number of data elements by the size of each element. For example, the data in a single-dimension array consisting of 4 Integer data elements of 2 bytes each occupies 8 bytes. The 8 bytes required for the data plus the 24 bytes of overhead brings the total memory requirement for the array to 32 bytes.

    A Variant containing an array requires 12 bytes more than the array alone.

    Note Use the StrConv function to convert one type of string data to another.


    --------------------------------------------------------------------------------
    Send feedback to MSDN.Look here for MSDN Online resources.

  10. #10
    WorkHorse
    Guest
    Originally posted by cafeenman
    I just want to be able to validate user input before VB does so I can return a real error message instead of the default error that VB would raise.
    Why not use VarType to validate whether the value can be passed to a certain data type?

  11. #11

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by WorkHorse
    Why not use VarType to validate whether the value can be passed to a certain data type?
    Because I forgot about its existence. Thanks.

  12. #12
    Member
    Join Date
    May 2002
    Posts
    54
    log on aim yo!

  13. #13

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by WorkHorse
    Why not use VarType to validate whether the value can be passed to a certain data type?
    VarType doesn't tell you what kind of data a string holds. It tells you what kind of data is in a variable. You would use it like this:
    VB Code:
    1. Dim s As String
    2.  
    3. MsgBox VarType(s)
    4. ' Returns 8

    Not like this (which is what I need)

    VB Code:
    1. MsgBox VarType("6.379")
    2. ' Also returns 8 because it's a string which is
    3. ' what a text box holds
    4.  
    5. ' if I coerce the text to another variable before using vartype
    6. ' then I lose the opportunity to validate it before VB does

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by John McKernan
    Just off the top of my head, maybe something like...
    VB Code:
    1. public const dblMax as Double=1.79769313486232^308
    Just a word of caution if someone else tries this, I dont think
    Mathematicaly these two ARE the same:

    1.79769313486232^308
    1.79769313486232E +308

    The first will be a very large Number, 2833..... 75 values to follow,
    where as the second is 179769..... with 303 values to follow.

    I could be wrong

  15. #15

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by Bruce Fox


    Just a word of caution if someone else tries this, I dont think
    Mathematicaly these two ARE the same:

    1.79769313486232^308
    1.79769313486232E +308

    The first will be a very large Number, 2833..... 75 values to follow,
    where as the second is 179769..... with 303 values to follow.

    I could be wrong
    No Bruce, I think you are absolutely right. This validation scheme of mine is not well conceived at all. I need a better way to do it. VB will raise an error, and I could maybe implement that, but I'd really like to catch the error before VB so I can give the user specific information rather than just letting VB tell them that an error occurred somewhere. I.e. Type Mismatch. Most users say "Huh?"

  16. #16
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Just a thought from left field, (may generate possibilities

    Considering the High end for a moment, all it is a Large Number
    1.7???E +308!
    So, in theory, what if you were to divide it and find a suitable
    number, so you can apply that as a "Number" mathematicaly
    to a user input to test if the input is outa range....
    (Yes, I have confussed myself too)

  17. #17
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    You know, ...you could write a program that incriments the smallest possible decimal value allowed in a double. Start it at 1.3E+308 and let it run like...all night...and trap the overflow error when it occurs. Voila your maximum value. Just a rough idea, considering its 4AM up here...
    You just proved that sig advertisements work.

  18. #18

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by nishantp
    You know, ...you could write a program that incriments the smallest possible decimal value allowed in a double. Start it at 1.3E+308 and let it run like...all night...and trap the overflow error when it occurs. Voila your maximum value. Just a rough idea, considering its 4AM up here...
    I already know what the number is. I just don't know how to code it as a constant.

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