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 ); 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; 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 ); 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. Exampleu64 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 */ DescriptionConverts a signed integer into an unsigned integer of the same size. The value may change drasticly. Return ValuesThe unsigned integer. ErrorsNone. Examples64 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 */ 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; _V3_SET64( a, FFFFFFFF, FFFFFFFF ); b = v3s64u64( a ); /* b = -1 */ v3AddSyntax#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 ); DescriptionAdds two integers of specified type and returns the result. Return ValuesThe sum of the two arguments. ErrorsNone. Exampleu64 expenses = v3u64u32( 45 ); u64 fees = v3u64u32( 102 ); u64 total_costs; total_costs = v3u64Add( fees, expenses ); v3SubSyntax#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 ); DescriptionSubtracts two integers of specified type and returns the result. Return ValuesThe difference of the two arguments. ErrorsNone. Exampleu64 income = v3u64u32( 235 ); u64 expenses = v3u64u32( 19 ); u64 net; net = v3u64Sub( income, expenses ); v3MulSyntax#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 ); DescriptionMultiplies two integers of specified type and returns the result. Return ValuesThe product of the two arguments. ErrorsNone. Exampleu64 length = v3u64u32( 7 ); u64 width = v3u64u32( 13 ); u64 area; area = v3u64Mul( length, width ); v3DivSyntax#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 ); 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. Exampleu64 candies = v3u64u32( 100 ); u64 children = v3u64u32( 5 ); u64 candies_each; candies_each = v3u64Div( candies, children ); v3ModSyntax#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 ); 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 = v3u64u32( 52 ); u64 players = v3u64u32( 7 ); u64 leftover_cards; leftover_cards = v3u64Mod( cards, players ); v3IncSyntax#include "cosmmath.h" u64 v3u64Inc( u64 * a ); s64 v3s64Inc( s64 * a ); u128 v3u128Inc( u128 * a ); s128 v3s128Inc( 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. Exampleu64 i, j; _V3_SET64( i, 01234567, 89ABCDEF ); j = v3u64Inc( &i ); /* i is now 0x0123456789ABCDF0 and j is 0x0123456789ABCDEF same as: j = i++; */ v3DecSyntax#include "cosmmath.h" u64 v3u64Dec( u64 * a ); s64 v3s64Dec( s64 * a ); u128 v3u128Dec( u128 * a ); s128 v3s128Dec( 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. Exampleu64 i; _V3_SET64( i, 13845A34, 09AB4DEF ); j = v3u64Dec( &i ); /* i is now 0x0123456789ABCDEE and j is 0x0123456789ABCDEF same as: j = i--; */ v3EqSyntax#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 ); DescriptionChecks the equality of two integers of specified type. Return Values1 if the numbers are equal, or 0 if they are not equal. ErrorsNone. Exampleu64 bytes_received = v3u64u32( 45 ); u64 bytes_sent = v3u64u32( 39 ); u32 all_sent; all_sent = v3u64Eq( bytes_received, bytes_sent ); v3GtSyntax#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 ); DescriptionChecks to see if the first integer of specified type is greater than the second. Return Values1 if the first number is greater than the second, otherwise 0. ErrorsNone. Exampleu64 chairs = v3u64u32( 45 ); u64 people = v3u64u32( 39 ); u32 enough_chairs; enough_chairs = v3u64Gt( chairs, people ); /* enough_chairs = 1 */ v3LtSyntax#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 ); DescriptionChecks to see if the first integer of specified type is less than the second. Return Values1 if the first number is less than the second, otherwise 0. ErrorsNone. Exampleu64 uv_rays = v3u64u32( 27 ); u64 max = v3u64u32( 46 ); u32 no_sunburn; no_sunburn = v3u64Lt( uv_rays, max ); /* no_sunburn = 1 */ v3AndSyntax#include "cosmmath.h" u64 v3u64And( u64 a, u64 b ); u128 v3u128And( u128 a, u128 b ); DescriptionPerforms the logical AND of two integers of specified type. Return ValuesThe logical AND of the two arguments. ErrorsNone. Exampleu64 foo = v3u64u32( 0x0000001C ); u64 bar = v3u64u32( 0x000001E3 ); u64 go; go = v3u64And( foo, bar ); v3OrSyntax#include "cosmmath.h" u64 v3u64Or( u64 a, u64 b ); u128 v3u128Or( u128 a, u128 b ); DescriptionPerforms the logical OR of two integers of specified type. Return ValuesThe logical OR of the two arguments. ErrorsNone. Exampleu64 foo = v3u64u32( 0x000002E3 ); u64 bar = v3u64u32( 0x0000008F ); u64 go; go = v3u64Or( foo, bar ); v3XorSyntax#include "cosmmath.h" u64 v3u64Xor( u64 a, u64 b ); u128 v3u128Xor( 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. Exampleu64 foo = v3u64u32( 0x021C4F19 ); u64 bar = v3u64u32( 0x00074B31 ); u64 go; go = v3u64Xor( foo, bar ); v3NotSyntax#include "cosmmath.h" u64 v3u64Not( u64 a ); u128 v3u128Not( u128 a ); DescriptionPerforms the logical NOT of two integers of specified type. Return ValuesThe logical NOT of the argument. ErrorsNone. Exampleu64 foo = v3u64u32( 0x00F0401C ); u64 notfoo; notfoo = v3u64Not( foo ); v3LshSyntax#include "cosmmath.h" u64 v3u64Lsh( u64 a, u32 x ); u128 v3u128Lsh( u128 a, u32 x ); DescriptionShifts an integer of specified type by x digits to the left. Return ValuesThe shifted integer. ErrorsNone. Exampleu64 foo = v3u64u32( 0x300E481C ); u64 fooshifted; fooshifted = v3u64Lsh( foo, (u32) 4 ); /* fooshifted = 0x0000000300E481C0 */ v3RshSyntax#include "cosmmath.h" u64 v3u64Rsh( u64 a, u32 x ); u128 v3u128Rsh( u128 a, u32 x ); DescriptionShifts an integer of specified type by x digits to the right. Return ValuesThe shifted integer. ErrorsNone. Exampleu64 foo = v3u64u32( 0x000F061C ); u64 fooshifted; fooshifted = v3u64Rsh( foo, (u32) 4 ); /* fooshifted = 0x000000000000F061 */ v3f64NaNSyntax#include "cosmmath.h" s32 v3f64NaN( f64 number ); DescriptionTest if number is NAN. Return Values1 if number is NAN, 0 otherwise. ErrorsNone. Examplef64 a; /* some math */ if ( v3f64NaN( a ) ) { /* error, a isn't a number anymore */ } v3f64InfSyntax#include "cosmmath.h" s32 v3f64Inf( f64 number ); DescriptionTest if number is +/- Inf. Return Values1 if number is +Inf, -1 if -Inf, 0 otherwise. ErrorsNone. Examplef64 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.
|