Table of Contents

Class MathExtensions

Namespace
Typhon.Engine
Assembly
Typhon.Engine.dll

Formatting and small numeric helpers — human-friendly renderings of sizes, counts, durations, and bandwidth, plus a few power-of-two utilities. Formatting uses a fixed en-us culture so output is stable regardless of the host locale.

public static class MathExtensions
Inheritance
MathExtensions
Inherited Members

Methods

Bandwidth(int, double)

Formats a throughput as a human-readable rate — size divided by elapsed seconds, scaled 1024-based with bit-style units, e.g. 1.5Mb/sec.

public static string Bandwidth(int size, double elapsed)

Parameters

size int

Amount transferred (bytes).

elapsed double

Elapsed time, in seconds.

Returns

string

The rate string, in the fixed en-us culture.

Bandwidth(long, double)

Formats a throughput as a human-readable rate — size divided by elapsed seconds, scaled 1024-based with bit-style units, e.g. 1.5Mb/sec.

public static string Bandwidth(long size, double elapsed)

Parameters

size long

Amount transferred (bytes).

elapsed double

Elapsed time, in seconds.

Returns

string

The rate string, in the fixed en-us culture.

FriendlyAmount(double)

Formats a count with a 1000-based scale suffix (none, K, M, G).

public static string FriendlyAmount(this double val)

Parameters

val double

The value to format.

Returns

string

The formatted string, in the fixed en-us culture.

FriendlyAmount(int)

Formats a count with a 1000-based scale suffix (none, K, M, B), e.g. 1.5M for 1,500,000.

public static string FriendlyAmount(this int val)

Parameters

val int

The value to format.

Returns

string

The formatted string, in the fixed en-us culture.

FriendlySize(double)

Formats a value with a 1024-based scale using bit-style units (b, Kb, Mb, Gb).

public static string FriendlySize(this double val)

Parameters

val double

The value to format.

Returns

string

The formatted string, in the fixed en-us culture.

FriendlySize(int)

Formats a byte/element count with a 1024-based scale suffix (none, K, M, B), e.g. 1.5M.

public static string FriendlySize(this int val)

Parameters

val int

The value to format.

Returns

string

The formatted string, in the fixed en-us culture.

FriendlySize(long)

Formats a byte/element count with a 1024-based scale suffix (none, K, M, B), e.g. 1.5M.

public static string FriendlySize(this long val)

Parameters

val long

The value to format.

Returns

string

The formatted string, in the fixed en-us culture.

FriendlyTime(double, bool)

Formats a duration (in seconds) with the largest fitting unit — sec, ms, µs, or ns. When displayRate is true, appends the reciprocal as a per-second rate, e.g. 1.5µs (666.667K/sec).

public static string FriendlyTime(this double val, bool displayRate = true)

Parameters

val double

The duration, in seconds.

displayRate bool

When true, also append the 1/val rate in parentheses.

Returns

string

The formatted string, in the fixed en-us culture.

IsPowerOf2(int)

Whether x is a power of two. Returns true for 0.

public static bool IsPowerOf2(this int x)

Parameters

x int

The value to test.

Returns

bool

true if x has at most one bit set.

IsPowerOf2(long)

Whether x is a power of two. Returns true for 0.

public static bool IsPowerOf2(this long x)

Parameters

x long

The value to test.

Returns

bool

true if x has at most one bit set.

NextPowerOf2(int)

Return the next power of 2 of the given value

public static int NextPowerOf2(this int v)

Parameters

v int

The value

Returns

int

The next power of 2

Remarks

If the given value is already a power of 2, this method will return the next one.

TicksToSeconds(long)

Converts TimeSpan ticks to seconds.

public static double TicksToSeconds(this long ticks)

Parameters

ticks long

A tick count (100 ns units).

Returns

double

The equivalent number of seconds.

TotalSeconds(int)

Converts TimeSpan ticks to total seconds via FromTicks(long).

public static double TotalSeconds(this int ticks)

Parameters

ticks int

A tick count (100 ns units).

Returns

double

The equivalent number of seconds.

TotalSeconds(long)

Converts TimeSpan ticks to total seconds via FromTicks(long).

public static double TotalSeconds(this long ticks)

Parameters

ticks long

A tick count (100 ns units).

Returns

double

The equivalent number of seconds.