MulDiv Function

Declare Function MulDiv Lib "kernel32.dll" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long

Platforms

Description & Usage

MulDiv multiplies two 32-bit integers together and divides the resulting 64-bit integer by a 32-bit integer. The final result is (usually) a 32-bit integer. This function is useful because it can calculate expressions which, although resulting in a perfectly acceptable value, would otherwise cause an overflow error if the programming language's arithmetic operators were used. If the end result is not an integer, it is rounded to the nearest integer.

Return Value

If an error occured (such as if the result is greater than a 32-bit value or division by zero was attempted), the function returns -1. If successful, the function returns the result of the expression (nNumber * nNumerator) / nDenominator rounded to the nearest integer.

Visual Basic-Specific Issues

None.

Parameters

nNumber
The first number to multiply.
nNumerator
The second number to multiply.
nDenominator
The number to divide by.

Example

' This code is licensed according to the terms and conditions listed here.

' Demonstrate how MulDiv can help avoid producing overflow errors.
Dim result As Long

' Without MulDiv: If you uncomment the line below, an overflow error
' will occur because the result of the multiplication is greater
' than a 32-bit value.
'result = -134217728 * 243 / 110592

' With MulDiv: There is no problem calculating the result.
result = MulDiv(-134217728, 243, 110592)
' (The result is exactly -294912.)

Category

Math

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: October 9, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/m/muldiv.html