Double

From Xojo Documentation

Data Type


A Double is an intrinsic data type. A Double is a number that can contain a decimal value, i.e., a real number. In other languages, a Double may be referred to as a double precision floating point number. Because Doubles are numbers, you can perform mathematical calculations on them. Doubles use 8 bytes of memory. The default value of a Double is 0.0.

Methods
Equals ToString ToText
Shared Methods
FromString FromText Parse

Notes

Double is an IEEE double-precision, floating-point value. This means it is speedy but has some limits for values it can contain. For more information, refer to the wikipedia page about floating point or the Floating Point Primer blog post.

In most situations you should use Currency when dealing with monetary values.

The VarType function returns a value of 5 when passed a Double.

Numerical limits

  • The maximum value of a Double is: ±1.79769313486231570814527423731704357e+308
  • The minimum value towards zero is: ±4.94065645841246544176568792868221372e-324

NaN and Infinity

Doubles can hold some special values described below:

  • NaN (i.e. "Not a Number"): occurs if you attempted to perform an illegal mathematical operation, like getting the square root of a negative number. Any further calculation made with a NaN will lead to a NaN value. Str or Format methods return a string beginning with "NaN", e.g. "NaN(021)".
  • Infinity: some calculations lead to an infinite result (positive or negative), e.g. Log( 0 ), or you may exceed the maximum value which can be hold. In such a case, a double will be set to a special value, whose Str will return "INF" (for INFinity) or "-INF" (negative INFinity). Any further calculation will lead to a NaN or infinity value.

Sample Code

The Double data type allows you to store and manage floating point numbers.

Var d As Double
d = 3.141592653

As Double using IEEE floating point there are some values that cannot be represented as a Double. For example, consider this code:

Var d1 As Double = 5.1
Var d2 As Double = d1 * 100
Var d3 As Double = Round(d1 * 100)

The value 5.1 cannot be stored directly in a Double (it's actually more like 5.0999999999999996). When you multiple that actually value by 100 you end up with 509.9999999999999432 (this is d2) when you are really expecting a value of 510. In this case you want to use the Round method to get the value you want (d3).

See Also

Var, Static, Currency, Single data types; Equals, IsNumeric, Mod, Str, Val, VarType functions; Double precision floating point format on Wikipedia, Floating Point Primer topics.