Example, Muldiv( ) function, Syntax – Echelon Neuron C User Manual

Page 135: Muldiv24( ) function

Advertising
background image

Neuron C Reference Guide

115

Example

msg_tag motor;
# define MOTOR_ON 0
# define ON_FULL 1

when (io_changes(switch1)to ON)
{

// Send a message to the motor

msg_out.tag = motor;

msg_out.code = MOTOR_ON;

msg_out.data[0] = ON_FULL;

msg_send();
}

muldiv( )

Function

The muldiv( ) function permits the computation of (

A

*

B

)/

C

where

A

,

B

, and

C

are

all 16-bit values, but the intermediate product of (

A

*

B

) is a 32-bit value. Thus,

the accuracy of the result is improved. There are two versions of this function:
muldiv( ) and muldivs( ). The muldiv( ) function uses unsigned arithmetic, while

the muldivs( ) function (see below) uses signed arithmetic.
See also muldiv24( ) and muldiv24s( ) for functions which use 24-bit intermediate
accuracy for faster performance.

Syntax

#include <stdlib.h>

unsigned long muldiv (unsigned long

A

, unsigned long

B

,

unsigned

long

C

);

Example

#include <stdlib.h>
unsigned long a, b, c, d;
...

void f(void)
{

d = muldiv(a, b, c); // d = (a*b)/c

}

muldiv24( )

Function

The muldiv24( ) function permits the computation of (

A

*

B

)/

C

where

A

is a 16-bit

value, and

B

and

C

are both 8-bit values, but the intermediate product of (

A

*

B

) is

a 24-bit value. Thus, the performance of the function is improved while
maintaining the accuracy of the result. There are two versions of this function:

muldiv24( ) and muldiv24s( ). The muldiv24( ) function uses unsigned

arithmetic, while the muldiv24s( ) function (see below) uses signed arithmetic.

See also muldiv( ) and muldivs( ) for functions which use 32-bit intermediate

accuracy for greater accuracy at the expense of slower performance. You can

Advertising