[Cosm Logo]

Math Functions


Cosm{bigger}{smaller}

Syntax

#include "cosm/os_math.h"
u128 CosmU128U32( u32 a );
s128 CosmS128S32( s32 a );
u128 CosmU128U64( u64 a );
s128 CosmS128S64( 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;
  u128 tiger;

  tiger = CosmU128U32( cat );

Cosm{smaller}{bigger}

Syntax

#include "cosm/os_math.h"
u32 CosmU32U128( u128 a );
s32 CosmS32S128( s128 a );
u64 CosmU64U128( u128 a );
s64 CosmS64S128( 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

  u128 pig;
  u32 ham;

  _COSM_SET128( pig, FEDCBA9876543210, FEDCBA9876543210 );

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

Cosm{unsigned}{signed}

Syntax

#include "cosm/os_math.h"
u128 CosmU128S128( 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

  s128 a;
  u128 b;

  a = ...;
  b = CosmU128S128( a );

Cosm{signed}{unsigned}

Syntax

#include "cosm/os_math.h"
s128 CosmS128U128( 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;

  _COSM_SET128( a, FFFFFFFFFFFFFFFF, FFFFFFFFFFFFFFFF )
  b = CosmS128U128( a );
  /* b = -1 */

CosmAdd

Syntax

#include "cosm/os_math.h"
u128 CosmU128Add( u128 a, u128 b );
s128 CosmS128Add( 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

  u128 expenses;
  u128 fees;
  u128 total_costs;

  /* ... */

  total_costs = CosmU128Add( fees, expenses );

CosmSub

Syntax

#include "cosm/os_math.h"
u128 CosmU128Sub( u128 a, u128 b );
s128 CosmS128Sub( 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

  u128 income;
  u128 expenses;
  u128 net;

  /* ... */

  net = CosmU128Sub( income, expenses );

CosmMul

Syntax

#include "cosm/os_math.h"
u128 CosmU128Mul( u128 a, u128 b );
s128 CosmS128Mul( 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

  u128 length;
  u128 width;
  u128 area;

  /* ... */

  area = CosmU128Mul( length, width );

CosmDiv

Syntax

#include "cosm/os_math.h"
u128 CosmU128Div( u128 a, u128 b );
s128 CosmS128Div( 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

  u128 candies;
  u128 children;
  u128 candies_each;

  /* ... */

  candies_each = CosmU128Div( candies, children );

CosmMod

Syntax

#include "cosm/os_math.h"
u128 CosmU128Mod( u128 a, u128 b );
s128 CosmS128Mod( 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;
  u64 players;
  u64 leftover_cards;

  /* ... */

  leftover_cards = CosmU128Mod( cards, players );

CosmDivMod

Syntax

#include "cosm/os_math.h"
void CosmU64DivMod( u64 a, u64 b, u64 * div, u64 * mod );
void CosmU128DivMod( u128 a, u128 b, u128 * div, u128 * mod );

Description

Combined version of the unsigned Div and Mod functions.
div = a / b;
mod = a % b;

Return Values

None.

Errors

If b is zero (divide by zero), 0 will be returned in both div and mod.

Example

  u64 cards;
  u64 players;
  u64 per_player;
  u64 leftover;

  CosmU64DivMod( cards, players, &per_player, &leftover );

CosmInc

Syntax

#include "cosm/os_math.h"
u128 CosmU128Inc( u128 * a );
s128 CosmS128Inc( 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

  u128 i;

  /* ... */

  CosmU128Inc( &i );

  /* i is now one more then it was */

CosmDec

Syntax

#include "cosm/os_math.h"
u128 CosmU128Dec( u128 * a );
s128 CosmS128Dec( 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

  u128 i;

  /* ... */

  CosmU128Inc( &i );

  /* i is now one less then it was */

CosmEq

Syntax

#include "cosm/os_math.h"
u32 CosmU128Eq( u128 a, u128 b );
u32 CosmS128Eq( 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

  u128 bytes_received;
  u128 bytes_sent;

  /* ... */

  if ( !CosmU128Eq( bytes_received, bytes_sent ) )
  {
    /* ... */
  }

CosmGt

Syntax

#include "cosm/os_math.h"
u32 CosmU128Gt( u128 a, u128 b );
u32 CosmS128Gt( s128 a, s128 b );

Description

Checks to see if the first integer of specified type is greater than the second. For greater than or equal use !CosmLt().

Return Values

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

Errors

None.

Example

  u128 bytes_received;
  u128 bytes_sent;

  /* ... */

  if ( CosmU128Gt( bytes_received, bytes_sent ) )
  {
    /* ... */
  }

CosmLt

Syntax

#include "cosm/os_math.h"
u32 CosmU128Lt( u128 a, u128 b );
u32 CosmS128Lt( s128 a, s128 b );

Description

Checks to see if the first integer of specified type is less than the second. For less than or equal use !CosmGt().

Return Values

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

Errors

None.

Example

  u128 bytes_received;
  u128 bytes_sent;

  /* ... */

  if ( CosmU128Lt( bytes_received, bytes_sent ) )
  {
    /* ... */
  }

CosmAnd

Syntax

#include "cosm/os_math.h"
u128 CosmU128And( 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

  u128 foo;
  u128 bar;
  u128 result;

  /* ... */

  result = CosmU128And( foo, bar );

CosmOr

Syntax

#include "cosm/os_math.h"
u128 CosmU128Or( 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

  u128 foo;
  u128 bar;
  u128 result;

  /* ... */

  result = CosmU128Or( foo, bar );

CosmXor

Syntax

#include "cosm/os_math.h"
u128 CosmU128Xor( 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

  u128 foo;
  u128 bar;
  u128 result;

  /* ... */

  result = CosmU128Xor( foo, bar );

CosmNot

Syntax

#include "cosm/os_math.h"
u128 CosmU128Not( u128 a );

Description

Performs the logical NOT of two integers of specified type.

Return Values

The logical NOT of the argument.

Errors

None.

Example

  u128 foo;
  u128 result;

  /* ... */

  result = CosmU128Not( foo );

CosmLsh

Syntax

#include "cosm/os_math.h"
u128 CosmU128Lsh( 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

  u128 foo;
  u128 fooshifted;

  /* ... */

  fooshifted = CosmU128Lsh( foo, 4 );

CosmRsh

Syntax

#include "cosm/os_math.h"
u128 CosmU128Rsh( 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

  u128 foo;
  u128 fooshifted;

  /* ... */

  fooshifted = CosmU128Lsh( foo, 4 );

CosmFloatNaN

Syntax

#include "cosm/os_math.h"
s32 CosmFloatNaN( 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 ( CosmFloatNaN( a ) )
  {
    /* error, a isn't a number anymore */
  }

CosmFloatInf

Syntax

#include "cosm/os_math.h"
s32 CosmFloatInf( 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 ( CosmFloatInf( a ) )
  {
    /* error, a got too large */
  }

© Copyright Mithral Communications & Design Inc. 1995-2017. All rights reserved. Mithral® and Cosm® are trademarks of Mithral Communications & Design Inc.
Document last modified: Jun 01, 2012