**math way**to

**exactly**translate a DOUBLE's fractional digits to a ULONGINT?

NOTE:

- I

*do not*want to

**convert**a DOUBLE to a ULONGINT !!!

I want to

**translate**a DOUBLE's

*fractional digits*to a ULONGINT.

e.g. 0.876399937 ==> 876399937ull

*but that is a lot of overhead*.

I can get there by multiplying a DOUBLE by a large ULONGINT CONST... but I would have to use a STRING version of the DOUBLE's fractional digits to get the length so that I could use the correct CONST for that set of digits. Because, without being able to choose the correct CONST for each set of digits, I sometimes end up with ULONGINT results that are

*too large*- or sometimes with

*rounding errors*. [see output examples below].

Code: Select all

`dim d as double`

dim dsu as ulongint

dim dmu as ulongint

'

print "Translate a DOUBLE's fractional digits to ULONGINT representation:"

print

print "1. RND() DOUBLE"

print "2. DOUBLE fractional digits to STRING to ULONGINT"

print "3. DOUBLE Multiplied by CONST to ULONGINT"

print

For i = 1 To 30

'RND() DOUBLE

d = RND()

'DOUBLE to STRING to ULONGINT - EXACT representation of the DOUBLE digits.

dsu = CULngInt(mid(str(d), 3))

'DOUBLE Multiplied by CONST to ULONGINT.

' Sometimes an EXACT representation of the DOUBLE digits.

' Sometimes a ROUNDED representation of the DOUBLE digits.

' Sometimes 10 to 100 times greater than a representation of the DOUBLE digits.

'NOTE:

' Attempted with various CONST values.

' Some result is always off.

dmu = d * 10000000000000000ull

'

print tab(9); d

print tab(12); dsu

print tab(12); dmu

print

next

Code: Select all

`' Output examples:`

'1. RND() DOUBLE

'2. DOUBLE fractional digits to STRING to ULONGINT

'3. DOUBLE Multiplied by CONST to ULONGINT

' 0.5324827746953815

' 5324827746953815

' 5324827746953815 'same

' 0.2956116099376231

' 2956116099376231

' 2956116099376232 'rounded up

' 0.6646097381599248

' 6646097381599248

' 6646097381599247 'rounded down

' 0.179675575112924

' 179675575112924

' 1796755751129240 'x10

' 0.55096355965361

' 55096355965361

' 5509635596536100 'x100