[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 tiger;

  tiger = 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;
  u32 ham;

  _V3_SET64( pig, FEDCBA98, 76543210 );

  ham = v3u32u64( pig );
  /* ham = 0x76543210 */

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 value may change drasticly.

Return Values

The unsigned integer.

Errors

None.

Example

  s64 a;
  u64 b;

  a = v3s64s32( -39 );
  b = v3u64s64( a );
  /* b = 0xFFFFFFFFFFFFFFD9 */

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 and value may change.

Return Values

The signed integer.

Errors

None.

Example

  u64 a;
  s64 b;

  _V3_SET64( a, FFFFFFFF, FFFFFFFF );
  b = v3s64u64( a );
  /* b = -1 */

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 = v3u64u32( 45 );
  u64 fees = v3u64u32( 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 = v3u64u32( 235 );
  u64 expenses = v3u64u32( 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 = v3u64u32( 7 );
  u64 width = v3u64u32( 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 = v3u64u32( 100 );
  u64 children = v3u64u32( 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 = v3u64u32( 52 );
  u64 players = v3u64u32( 7 );
  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
    same as: j = i++;
  */

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
    same as: j = i--;
  */

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 = v3u64u32( 45 );
  u64 bytes_sent = v3u64u32( 39 );
  u32 all_sent;

  all_sent = 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 = v3u64u32( 45 );
  u64 people = v3u64u32( 39 );
  u32 enough_chairs;

  enough_chairs = v3u64Gt( chairs, people );
  /* enough_chairs = 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 = v3u64u32( 27 );
  u64 max = v3u64u32( 46 );
  u32 no_sunburn;

  no_sunburn = v3u64Lt( uv_rays, max );
  /* no_sunburn = 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 = v3u64u32( 0x0000001C );
  u64 bar = v3u64u32( 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 = v3u64u32( 0x000002E3 );
  u64 bar = v3u64u32( 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 = v3u64u32( 0x021C4F19 );
  u64 bar = v3u64u32( 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 = v3u64u32( 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 = v3u64u32( 0x300E481C );
  u64 fooshifted;

  fooshifted = v3u64Lsh( foo, (u32) 4 );
  /* fooshifted = 0x0000000300E481C0 */

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 = v3u64u32( 0x000F061C );
  u64 fooshifted;

  fooshifted = v3u64Rsh( foo, (u32) 4 );
  /* fooshifted = 0x000000000000F061 */

v3f64NaN

Syntax

#include "cosmmath.h"
s32 v3f64NaN( f64 number );

Description

Test if number is NAN.

Return Values

1 if number is NAN, 0 otherwise.

Errors

None.

Example

  f64 a;

  /* some math */
  if ( v3f64NaN( a ) )
  {
    /* error, a isn't a number anymore */
  }

v3f64Inf

Syntax

#include "cosmmath.h"
s32 v3f64Inf( f64 number );

Description

Test if number is +/- Inf.

Return Values

1 if number is +Inf, -1 if -Inf, 0 otherwise.

Errors

None.

Example

  f64 a;

  /* some math */
  if ( v3f64Inf( a ) )
  {
    /* error, a got too large */
  }

© Copyright Mithral Communications & Design Inc. 1995-2003. All rights reserved. Mithral® and Cosm® are trademarks of Mithral Communications & Design Inc.
Document last modified: May 22, 2003