DataType

Double


Description

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

Name

Parameters

Returns

Shared

Equals

numValue As Double, maxUIps As Integer

Boolean

FromString

value As String, Optional locale As Locale

Double

IsInfinite

Boolean

IsNotANumber

Boolean

Parse

Double

ToString

Optional locale As Locale, Optional format As String

String

Method descriptions


Double.Equals

Equals(numValue As Double, maxUIps As Integer) As Boolean

Tests whether two Single or Double numbers are equal within a specified tolerance.

Use Equals rather than :doc:`=</api/code_execution/`greater_than_or_equal when you need to determine whether two floating point numbers are close enough in value to be considered “equal.” This can be used to account for the imprecision of floating point division on computers, for example. It allows for a user-specified rounding error.

For maxUIps, the last position refers to the the last byte in the binary representation of the mantissa. maxUIps is the amount of difference between the last byte of the 2 numbers that is still acceptable. For example, consider these 2 numbers:

3.1415926535897932 // last byte value is 0x18
3.141592653589795 // last byte value is 0x1C

A maxUIps of 3 will result in “not equal”, while a maxUIps of 4 will result in “equal”.

More information: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Compare two values:

// 2 double values that vary by a tiny bit
Var d1 As Double = 3.1415926535897935
Var d2 As Double = 3.1415926535897933

If d1 = d2 Then
   // Will not get here because they are not an exact match
End If

If d1.Equals(d2, 1) Then
   // Will get here because they are "close enough"
End If

Double.FromString

FromString(value As String, Optional locale As Locale) As Double

Converts a String value containing a number that can be represented as a Double to a Double value.

This method is shared.

If no locale is specified, then Locale.Raw is used.

Convert values to the Double type:

Var userValue As String
userValue = "123.45"

Var d As Double
d = Double.FromString(userValue)

Var locale As New Locale("en-US")
Var value As Double

userValue = "123.45"
value = Double.FromString(userValue, locale) ' value = 123.45

Double.IsInfinite

IsInfinite As Boolean

Returns True if the value is infinite.


Double.IsNotANumber

IsNotANumber As Boolean

Returns True if the value is Not-A-Number.


Double.Parse

Parse As Double

Converts a String to a Double value.

This method is shared.

If no locale is specified, then Locale.Raw is used.

Numbers are converted only if they are found at the beginning of the text. Any numbers that follow a non-numeric value are ignored. Empty text returns 0.

Var d As Double
d = Double.Parse("123ABC")
// d = 123

Double.ToString

ToString(Optional locale As Locale, Optional format As String) As String

Converts a Double value to a String value using the supplied locale and format.

If no locale is specified, then Locale.Raw is used.

Refer to Unicode Number Format Patterns for a list of formats.

Convert Double values to String values:

Var d As Double = 123.45

Var s As String
s = d.ToString // s = "123.45"

Var n As Double = 1239.4567
Var s1 As String = n.ToString(Locale.Current, "#,###.##") // s1 = 1,239.46

Var n2 As Double = 12
Var s2 As String = n.ToString(Locale.Current, "#.00") // s2 = 12.00

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.

You can test for these values using the IsNotANumber and IsInfinite methods.

Sample code

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

Var d As Double
d = 3.141592653

Compatibility

All projects types on all supported operating systems.