## calculating and displaying big numbers problem

## calculating and displaying big numbers problem

(OP)

Hello everybody,

I have a problem of calculating and displaying big numbers

this simple code will explain the idea:

!=================================================================

!-- Direct calculation--

stop(10239421*8*512) ! gives wrong answer (negative)

!-----------------------------------------

!--define n as Long--

n=10239421*8*512

stop(n) ! gives wrong answer (negative)

!-----------------------------------------

!--define n1 as Decimal--

n1=10239421*8*512

stop(n1) ! gives wrong answer (negative)

!-----------------------------------------

! I tried to do the operation in two steps

n1=10239421*8

n1=n1*512

stop(n1) ! gives correct answer

(which is not applicable all time)

!-----------------------------------------

I faced this problem when I tried to use DIRECTORY function to get a list of file, so the file sizes which is so big give a result of nigative value

Any notes is appreciated

I have a problem of calculating and displaying big numbers

this simple code will explain the idea:

!=================================================================

!-- Direct calculation--

stop(10239421*8*512) ! gives wrong answer (negative)

!-----------------------------------------

!--define n as Long--

n=10239421*8*512

stop(n) ! gives wrong answer (negative)

!-----------------------------------------

!--define n1 as Decimal--

n1=10239421*8*512

stop(n1) ! gives wrong answer (negative)

!-----------------------------------------

! I tried to do the operation in two steps

n1=10239421*8

n1=n1*512

stop(n1) ! gives correct answer

(which is not applicable all time)

!-----------------------------------------

I faced this problem when I tried to use DIRECTORY function to get a list of file, so the file sizes which is so big give a result of nigative value

Any notes is appreciated

## RE: calculating and displaying big numbers problem

10239421 * 8 * 512 = 41,940,668,416

Max value in LONG (signed integer) = 2,147,483,647

Max value in ULONG (unsigned integer) = 4,294,967,295

Both LONG & ULONG are insufficient for storing the value.

What was the DECIMAL length you used? Ideally I would use DECIMAL(13,0). The following works fine since I have used variables for the values.

## CODE

intRes2 ULONG

decRes DECIMAL(13,0)

fltRes REAL

Var1 ULONG(10239421)

Var2 ULONG(8)

Var3 ULONG(512)

CODE

intRes1 = Var1 * Var2 * Var3

intRes2 = Var1 * Var2 * Var3

decRes = Var1 * Var2 * Var3

fltRes = Var1 * Var2 * Var3

MESSAGE('LONG = ' & intRes1 & |

'|ULONG = ' & intRes2 & |

'|DECIMAL = ' & decRes & |

'|REAL = ' & fltRes, 'Results')

RETURN

Regards

## RE: calculating and displaying big numbers problem

Forgot to explain ::

There are two types of arithmetic used by the Clarion RTL, integer & decimal maths. By using LONGs or constant values, the RTL uses integer maths (which is faster than decimal maths) and hence the result gets truncated. By using ULONGs, you are forcing decimal maths and hence the values are correct.

Regards

## RE: calculating and displaying big numbers problem

You can also force decimal maths in LONGs by using 1.0 as the first variable to be multiplied as shown below ::

## CODE

intRes2 ULONG

decRes DECIMAL(13,0)

fltRes REAL

Var1 LONG(10239421)

Var2 LONG(8)

Var3 LONG(512)

CODE

intRes1 = 1.0 * Var1 * Var2 * Var3

intRes2 = 1.0 * Var1 * Var2 * Var3

decRes = 1.0 * Var1 * Var2 * Var3

fltRes = 1.0 * Var1 * Var2 * Var3

MESSAGE('LONG = ' & intRes1 & |

'|ULONG = ' & intRes2 & |

'|DECIMAL = ' & decRes & |

'|REAL = ' & fltRes, 'Results')

RETURN

Regards

## RE: calculating and displaying big numbers problem

Lots of thanks

Regards

## RE: calculating and displaying big numbers problem

I did modified my programs which facing this problem and it is fine , but still I cant get the correct size of files exceeding the limit of LONG type (2,147,483,647)

Iam using the DIRECTORY function to get the files sizes but the big files size come in minus with a wrong figure.

I tried to modify the FIL:Size from Long to Decimal(20) in EQUATES.CLW but it didnt work.

## RE: calculating and displaying big numbers problem

You cannot change it to DECIMAL(20) as a decimal(20) uses 11 bytes of storage as compared to a LONG which uses 4 bytes of storage. You can try changing it to a ULONG which will help you with files of sizes up to 4GB but beyond that you need to use the Windows API to find the file size.

What version of clarion do you use?

Regards

## RE: calculating and displaying big numbers problem

Thank you

## RE: calculating and displaying big numbers problem

If you have CWUTIL.inc & CWUTIL.clw in the <ClarionHome>\LIBSRC folder, make the following the changes ::

CWUTIL.inc (within the MODULE('cwutil.clw') structure (after GetFileDate definition))

----------

## CODE

CWUTIL.clw (after GetFileDate procedure)

----------

## CODE

!GetFileSize

! Get the system File Size for the specified file.

!-----------------------------------------------------------------------------!

GetFileSize PROCEDURE (STRING FileName)

hFindHandle LONG,AUTO

gFileFind GROUP( WIN32_FIND_DATA ),AUTO.

szFindFile CSTRING(FILE:MaxFileName),AUTO

fFileSize REAL

CODE

hFindHandle = 0

fFileSize = -1

szFindFile = CLIP (FileName)

CLEAR (gFileFind)

hFindHandle = FindFirstFile( szFindFile, gFileFind )

IF hFindHandle = INVALID_LONG_VALUE OR hFindHandle <= 0

RETURN fFileSize

END

FindClose (hFindHandle)

fFileSize = (gFileFind.nFileSizeHigh * 4294967296) + gFileFind.nFileSizeLow

RETURN fFileSize

Usage

-----

## CODE

FilesQ QUEUE(FILE:Queue),PRE(FIL)

BigFileSize REAL

END

CODE

DIRECTORY(FilesQ, 'D:\Virtual Machines\ShankHome_XP\*.VMDK', FILE:LongName+FILE:KeepDir)

LOOP F# = 1 TO RECORDS(FilesQ)

GET(FilesQ, F#)

IF FIL:Size < 0

FIL:BigFileSize = GetFileSize('D:\Virtual Machines\ShankHome_XP\' & CLIP(FIL:Name))

MESSAGE(CLIP(FIL:Name) & ' - File Size Changed from ' & FIL:Size & ' to ' & FIL:BigFileSize)

END

END

RETURN

Regards

## RE: calculating and displaying big numbers problem

You are great .. everything is running fine

thank you

## RE: calculating and displaying big numbers problem

I have one last problem in this topic

Iam trying to get the total disk space and free disk space for a drive using GetDiskFreeSpace API function.

for big drives I still have negative value and the rest is ok.

Thank you in advance

## RE: calculating and displaying big numbers problem

You need to maybe use the GetDiskFreeSpaceEx API.

Check out the DriveInfo class at http://roel-karin.abspoel.nl/?p=17&lang=en.

And, pay attention to how LARGE_INTERGERs are handled.

Regards

## RE: calculating and displaying big numbers problem