[Cosm Logo]

Math Functions


v3{bigger}{smaller}

Syntax

#include "cosmmath.h"
u64 v3u64u32( u32 a );
s64 v3s64s32( s32 a );
u128 v3u128u32( u32 a );
s128 v3s128s32( s32 a );
u128 v3u128u64( u64 a );
s128 v3s128s64( s64 a );

Description

Converts an integer into a larger sized integer. Cannot convert signed to unsigned, or unsigned to signed.

Return Values

The larger sized integer.

Errors

None.

Example

  u32 cat = 39;
  u64 dog;

  dog = v3u64u32( cat );

v3{smaller}{bigger}

Syntax

#include "cosmmath.h"
u32 v3u32u64( u64 a );
s32 v3s32s64( s64 a );
u32 v3u32u128( u128 a );
s32 v3s32s128( s128 a );
u64 v3u64u128( u128 a );
s64 v3s64s128( s128 a );

Description

Converts an integer into a smaller sized integer. Truncation will occur. Cannot convert signed to unsigned, or unsigned to signed.

Return Values

The smaller sized integer.

Errors

None.

Example

  u64 pig = 39;
  u32 ham;

  ham = v3u32u64( pig );

v3{unsigned}{signed}

Syntax

#include "cosmmath.h"
u64 v3u64s64( s64 a ); /* u64 <- s64 */
u128 v3u128s128( s128 a ); /* u128 <- s128 */

Description

Converts a signed integer into an unsigned integer of the same size. The sign may change.

Return Values

The unsigned integer.

Errors

None.

Example

  s64 a = -39;
  u64 b;

  b = v3u64u64( a );

v3{signed}{unsigned}

Syntax

#include "cosmmath.h"
s64 v3s64u64( u64 a ); /* s64 <- u64 */
s128 v3s128u128( u128 a ); /* s128 <- u128 */

Description

Converts an unsigned integer into a signed integer of the same size. The sign may change.

Return Values

The signed integer.

Errors

None.

Example

  u64 a = 27;
  s64 b;

  b = v3u64u64( a );

v3Add

Syntax

#include "cosmmath.h"
u64 v3u64Add( u64 a, u64 b );
s64 v3s64Add( s64 a, s64 b );
u128 v3u128Add( u128 a, u128 b );
s128 v3s128Add( s128 a, s128 b );

Description

Adds two integers of specified type and returns the result.

Return Values

The sum of the two arguments.

Errors

None.

Example

  u64 expenses = 45;
  u64 fees = 102;
  u64 total_costs;

  total_costs = v3u64Add( fees, expenses );

v3Sub

Syntax

#include "cosmmath.h"
u64 v3u64Sub( u64 a, u64 b );
s64 v3s64Sub( s64 a, s64 b );
u128 v3u128Sub( u128 a, u128 b );
s128 v3s128Sub( s128 a, s128 b );

Description

Subtracts two integers of specified type and returns the result.

Return Values

The difference of the two arguments.

Errors

None.

Example

  u64 income = 235;
  u64 expenses = 19;
  u64 net;

  net = v3u64Sub( income, expenses );

v3Mul

Syntax

#include "cosmmath.h"
u64 v3u64Mul( u64 a, u64 b );
s64 v3s64Mul( s64 a, s64 b );
u128 v3u128Mul( u128 a, u128 b );
s128 v3s128Mul( s128 a, s128 b );

Description

Multiplies two integers of specified type and returns the result.

Return Values

The product of the two arguments.

Errors

None.

Example

  u64 length = 7;
  u64 width = 13;
  u64 area;

  area = v3u64Mul( length, width );

v3Div

Syntax

#include "cosmmath.h"
u64 v3u64Div( u64 a, u64 b );
s64 v3s64Div( s64 a, s64 b );
u128 v3u128Div( u128 a, u128 b );
s128 v3s128Div( s128 a, s128 b );

Description

Divides two integers of specified type and returns the result.

Return Values

The quotient of the two arguments.

Errors

If b is zero (divide by zero), 0 will be returned.

Example

  u64 candies = 100;
  u64 children = 5;
  u64 candies_each;

  candies_each = v3u64Div( candies, children );

v3Mod

Syntax

#include "cosmmath.h"
u64 v3u64Mod( u64 a, u64 b );
s64 v3s64Mod( s64 a, s64 b );
u128 v3u128Mod( u128 a, u128 b );
s128 v3s128Mod( s128 a, s128 b );

Description

Finds the residue class of a mod b.

Return Values

0 to |b|-1.

Errors

If b is zero (divide by zero), 0 will be returned.

Example

  u64 cards = 52;
  u64 players = 102;
  u64 leftover_cards;

  leftover_cards = v3u64Mod( cards, players );

v3Inc

Syntax

#include "cosmmath.h"
u64 v3u64Inc( u64 * a );
s64 v3s64Inc( s64 * a );
u128 v3u128Inc( u128 * a );
s128 v3s128Inc( s128 * a );

Description

Adds one to an integer of specified type, by reference.

Return Values

The value of a BEFORE it is incremented. a is incremented, possibly wrapping the value of a. The return value is equivalent to a++.

Errors

None.

Example

  u64 i, j;

  _V3_SET64( i, 01234567, 89ABCDEF );

  j = v3u64Inc( &i );
  /*
    i is now 0x0123456789ABCDF0
    and j is 0x0123456789ABCDEF
  */

v3Dec

Syntax

#include "cosmmath.h"
u64 v3u64Dec( u64 * a );
s64 v3s64Dec( s64 * a );
u128 v3u128Dec( u128 * a );
s128 v3s128Dec( s128 * a );

Description

Subtracts one from an integer of specified type, by reference.

Return Values

The value of a BEFORE it is decremented. a is decremented, possibly wrapping the value of a. The return value is equivalent to a--.

Errors

None.

Example

  u64 i;

  _V3_SET64( i, 13845A34, 09AB4DEF );

  j = v3u64Dec( &i );
  /*
    i is now 0x0123456789ABCDEE
    and j is 0x0123456789ABCDEF
  */

v3Eq

Syntax

#include "cosmmath.h"
u32 v3u64Eq( u64 a, u64 b );
u32 v3s64Eq( s64 a, s64 b );
u32 v3u128Eq( u128 a, u128 b );
u32 v3s128Eq( s128 a, s128 b );

Description

Checks the equality of two integers of specified type.

Return Values

1 if the numbers are equal, or 0 if they are not equal.

Errors

None.

Example

  u64 bytes_received = 45;
  u64 bytes_sent = 39;
  u32 alldatasent;

  alldatasent = v3u64Eq( bytes_received, bytes_sent );

v3Gt

Syntax

#include "cosmmath.h"
u32 v3u64Gt( u64 a, u64 b );
u32 v3s64Gt( s64 a, s64 b );
u32 v3u128Gt( u128 a, u128 b );
u32 v3s128Gt( s128 a, s128 b );

Description

Checks to see if the first integer of specified type is greater than the second.

Return Values

1 if the first number is greater than the second, otherwise 0.

Errors

None.

Example

  u64 chairs = 45;
  u64 people = 39;
  u32 enough_chairs;

  enough_chairs = v3u64Gt( chairs, people );

enough_chairs would be 1.


v3Lt

Syntax

#include "cosmmath.h"
u32 v3u64Lt( u64 a, u64 b );
u32 v3s64Lt( s64 a, s64 b );
u32 v3u128Lt( u128 a, u128 b );
u32 v3s128Lt( s128 a, s128 b );

Description

Checks to see if the first integer of specified type is less than the second.

Return Values

1 if the first number is less than the second, otherwise 0.

Errors

None.

Example

  u64 uv_rays = 27;
  u64 max = 46;
  u32 no_sunburn;

  no_sunburn = v3u64Lt( uv_rays, max );

no_sunburn would be 1.


v3And

Syntax

#include "cosmmath.h"
u64 v3u64And( u64 a, u64 b );
u128 v3u128And( u128 a, u128 b );

Description

Performs the logical AND of two integers of specified type.

Return Values

The logical AND of the two arguments.

Errors

None.

Example

  u64 foo = 0x0000001C;
  u64 bar = 0x000001E3;
  u64 go;

  go = v3u64And( foo, bar );

v3Or

Syntax

#include "cosmmath.h"
u64 v3u64Or( u64 a, u64 b );
u128 v3u128Or( u128 a, u128 b );

Description

Performs the logical OR of two integers of specified type.

Return Values

The logical OR of the two arguments.

Errors

None.

Example

  u64 foo = 0x000002E3;
  u64 bar = 0x0000008F;
  u64 go;

  go = v3u64Or( foo, bar );

v3Xor

Syntax

#include "cosmmath.h"
u64 v3u64Xor( u64 a, u64 b );
u128 v3u128Xor( u128 a, u128 b );

Description

Performs the logical exclusive OR of two integers of specified type.

Return Values

The logical exclusive OR of the two arguments.

Errors

None.

Example

  u64 foo = 0x021C4F19;
  u64 bar = 0x00074B31;
  u64 go;

  go = v3u64Xor( foo, bar );

v3Not

Syntax

#include "cosmmath.h"
u64 v3u64Not( u64 a );
u128 v3u128Not( u128 a );

Description

Performs the logical NOT of two integers of specified type.

Return Values

The logical NOT of the argument.

Errors

None.

Example

  u64 foo = 0x00F0401C;
  u64 notfoo;

  notfoo = v3u64Not( foo );

v3Lsh

Syntax

#include "cosmmath.h"
u64 v3u64Lsh( u64 a, u32 x );
u128 v3u128Lsh( u128 a, u32 x );

Description

Shifts an integer of specified type by x digits to the left.

Return Values

The shifted integer.

Errors

None.

Example

  u64 foo = 0x300E481C;
  u64 fooshifted;

  fooshifted = v3u64Lsh( foo, (u32) 4 );

Would shift foo 4 digits left.


v3Rsh

Syntax

#include "cosmmath.h"
u64 v3u64Rsh( u64 a, u32 x );
u128 v3u128Rsh( u128 a, u32 x );

Description

Shifts an integer of specified type by x digits to the right.

Return Values

The shifted integer.

Errors

None.

Example

  u64 foo = 0x000F061C;
  u64 fooshifted;

  fooshifted = v3u64Rsh( foo, (u32) 2 );

Would shift foo 2 digits right.


© Copyright Mithral Communications & Design, Inc. 1999. All rights reserved. Mithral(tm) and Cosm(tm) are trademarks of Mithral Communications & Design, Inc.
Document last modified: