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 ); DescriptionConverts an integer into a larger sized integer. Cannot convert signed to unsigned, or unsigned to signed. Return ValuesThe larger sized integer. ErrorsNone. Exampleu32 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 ); DescriptionConverts an integer into a smaller sized integer. Truncation will occur. Cannot convert signed to unsigned, or unsigned to signed. Return ValuesThe smaller sized integer. ErrorsNone. Exampleu128 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 */ DescriptionConverts a signed integer into an unsigned integer of the same size. The value may change drasticly. Return ValuesThe unsigned integer. ErrorsNone. Examples128 a; u128 b; a = ...; b = CosmU128S128( a ); Cosm{signed}{unsigned}Syntax#include "cosm/os_math.h" s128 CosmS128U128( u128 a ); /* s128 <- u128 */ DescriptionConverts an unsigned integer into a signed integer of the same size. The sign and value may change. Return ValuesThe signed integer. ErrorsNone. Exampleu64 a; s64 b; _COSM_SET128( a, FFFFFFFFFFFFFFFF, FFFFFFFFFFFFFFFF ) b = CosmS128U128( a ); /* b = -1 */ CosmAddSyntax#include "cosm/os_math.h" u128 CosmU128Add( u128 a, u128 b ); s128 CosmS128Add( s128 a, s128 b ); DescriptionAdds two integers of specified type and returns the result. Return ValuesThe sum of the two arguments. ErrorsNone. Exampleu128 expenses; u128 fees; u128 total_costs; /* ... */ total_costs = CosmU128Add( fees, expenses ); CosmSubSyntax#include "cosm/os_math.h" u128 CosmU128Sub( u128 a, u128 b ); s128 CosmS128Sub( s128 a, s128 b ); DescriptionSubtracts two integers of specified type and returns the result. Return ValuesThe difference of the two arguments. ErrorsNone. Exampleu128 income; u128 expenses; u128 net; /* ... */ net = CosmU128Sub( income, expenses ); CosmMulSyntax#include "cosm/os_math.h" u128 CosmU128Mul( u128 a, u128 b ); s128 CosmS128Mul( s128 a, s128 b ); DescriptionMultiplies two integers of specified type and returns the result. Return ValuesThe product of the two arguments. ErrorsNone. Exampleu128 length; u128 width; u128 area; /* ... */ area = CosmU128Mul( length, width ); CosmDivSyntax#include "cosm/os_math.h" u128 CosmU128Div( u128 a, u128 b ); s128 CosmS128Div( s128 a, s128 b ); DescriptionDivides two integers of specified type and returns the result. Return ValuesThe quotient of the two arguments. ErrorsIf b is zero (divide by zero), 0 will be returned. Exampleu128 candies; u128 children; u128 candies_each; /* ... */ candies_each = CosmU128Div( candies, children ); CosmModSyntax#include "cosm/os_math.h" u128 CosmU128Mod( u128 a, u128 b ); s128 CosmS128Mod( s128 a, s128 b ); DescriptionFinds the residue class of a mod b. Return Values0 to |b|-1. ErrorsIf b is zero (divide by zero), 0 will be returned. Exampleu64 cards; u64 players; u64 leftover_cards; /* ... */ leftover_cards = CosmU128Mod( cards, players ); CosmDivModSyntax#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. Return ValuesNone. ErrorsIf b is zero (divide by zero), 0 will be returned in both div and mod. Exampleu64 cards; u64 players; u64 per_player; u64 leftover; CosmU64DivMod( cards, players, &per_player, &leftover ); CosmIncSyntax#include "cosm/os_math.h" u128 CosmU128Inc( u128 * a ); s128 CosmS128Inc( s128 * a ); DescriptionAdds one to an integer of specified type, by reference. Return ValuesThe value of a BEFORE it is incremented. a is incremented, possibly wrapping the value of a. The return value is equivalent to a++. ErrorsNone. Exampleu128 i; /* ... */ CosmU128Inc( &i ); /* i is now one more then it was */ CosmDecSyntax#include "cosm/os_math.h" u128 CosmU128Dec( u128 * a ); s128 CosmS128Dec( s128 * a ); DescriptionSubtracts one from an integer of specified type, by reference. Return ValuesThe value of a BEFORE it is decremented. a is decremented, possibly wrapping the value of a. The return value is equivalent to a--. ErrorsNone. Exampleu128 i; /* ... */ CosmU128Inc( &i ); /* i is now one less then it was */ CosmEqSyntax#include "cosm/os_math.h" u32 CosmU128Eq( u128 a, u128 b ); u32 CosmS128Eq( s128 a, s128 b ); DescriptionChecks the equality of two integers of specified type. Return Values1 if the numbers are equal, or 0 if they are not equal. ErrorsNone. Exampleu128 bytes_received; u128 bytes_sent; /* ... */ if ( !CosmU128Eq( bytes_received, bytes_sent ) ) { /* ... */ } CosmGtSyntax#include "cosm/os_math.h" u32 CosmU128Gt( u128 a, u128 b ); u32 CosmS128Gt( s128 a, s128 b ); DescriptionChecks to see if the first integer of specified type is greater than the second. For greater than or equal use !CosmLt(). Return Values1 if the first number is greater than the second, otherwise 0. ErrorsNone. Exampleu128 bytes_received; u128 bytes_sent; /* ... */ if ( CosmU128Gt( bytes_received, bytes_sent ) ) { /* ... */ } CosmLtSyntax#include "cosm/os_math.h" u32 CosmU128Lt( u128 a, u128 b ); u32 CosmS128Lt( s128 a, s128 b ); DescriptionChecks to see if the first integer of specified type is less than the second. For less than or equal use !CosmGt(). Return Values1 if the first number is less than the second, otherwise 0. ErrorsNone. Exampleu128 bytes_received; u128 bytes_sent; /* ... */ if ( CosmU128Lt( bytes_received, bytes_sent ) ) { /* ... */ } CosmAndSyntax#include "cosm/os_math.h" u128 CosmU128And( u128 a, u128 b ); DescriptionPerforms the logical AND of two integers of specified type. Return ValuesThe logical AND of the two arguments. ErrorsNone. Exampleu128 foo; u128 bar; u128 result; /* ... */ result = CosmU128And( foo, bar ); CosmOrSyntax#include "cosm/os_math.h" u128 CosmU128Or( u128 a, u128 b ); DescriptionPerforms the logical OR of two integers of specified type. Return ValuesThe logical OR of the two arguments. ErrorsNone. Exampleu128 foo; u128 bar; u128 result; /* ... */ result = CosmU128Or( foo, bar ); CosmXorSyntax#include "cosm/os_math.h" u128 CosmU128Xor( u128 a, u128 b ); DescriptionPerforms the logical exclusive OR of two integers of specified type. Return ValuesThe logical exclusive OR of the two arguments. ErrorsNone. Exampleu128 foo; u128 bar; u128 result; /* ... */ result = CosmU128Xor( foo, bar ); CosmNotSyntax#include "cosm/os_math.h" u128 CosmU128Not( u128 a ); DescriptionPerforms the logical NOT of two integers of specified type. Return ValuesThe logical NOT of the argument. ErrorsNone. Exampleu128 foo; u128 result; /* ... */ result = CosmU128Not( foo ); CosmLshSyntax#include "cosm/os_math.h" u128 CosmU128Lsh( u128 a, u32 x ); DescriptionShifts an integer of specified type by x digits to the left. Return ValuesThe shifted integer. ErrorsNone. Exampleu128 foo; u128 fooshifted; /* ... */ fooshifted = CosmU128Lsh( foo, 4 ); CosmRshSyntax#include "cosm/os_math.h" u128 CosmU128Rsh( u128 a, u32 x ); DescriptionShifts an integer of specified type by x digits to the right. Return ValuesThe shifted integer. ErrorsNone. Exampleu128 foo; u128 fooshifted; /* ... */ fooshifted = CosmU128Lsh( foo, 4 ); CosmFloatNaNSyntax#include "cosm/os_math.h" s32 CosmFloatNaN( f64 number ); DescriptionTest if number is NAN. Return Values1 if number is NAN, 0 otherwise. ErrorsNone. Examplef64 a; /* some math */ if ( CosmFloatNaN( a ) ) { /* error, a isn't a number anymore */ } CosmFloatInfSyntax#include "cosm/os_math.h" s32 CosmFloatInf( f64 number ); DescriptionTest if number is +/- Inf. Return Values1 if number is +Inf, -1 if -Inf, 0 otherwise. ErrorsNone. Examplef64 a; /* some math */ if ( CosmFloatInf( a ) ) { /* error, a got too large */ }
© Copyright Mithral Communications & Design Inc.
1995-2025.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|