Last active
April 5, 2018 08:05
-
-
Save kameko/4258f898371c92e303b6d0adc1fe6203 to your computer and use it in GitHub Desktop.
Non-throwing Division extention methods because I'm super paranoid of Division by Zero.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Caesura.Additions | |
{ | |
using System.Runtime.CompilerServices; | |
using Caesura.Additions.Contracts; | |
public static class Division | |
{ | |
/// <summary> | |
/// Attempt division between two numbers, if 'partition' is 0, return false. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return false.</param> | |
/// <param name="result">Result of the division.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Boolean TryDivision(this Int32 number, Int32 partition, out Int32 result) | |
{ | |
if (partition != 0) | |
{ | |
result = number / partition; | |
return true; | |
} | |
result = 0; | |
return false; | |
} | |
/// <summary> | |
/// Attempt division between two numbers. If 'partition' is 0, return 0. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return 0.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Int32 SafeDivision(this Int32 number, Int32 partition) | |
{ | |
if (partition != 0) | |
{ | |
return number / partition; | |
} | |
return 0; | |
} | |
/// <summary> | |
/// Attempt division between two numbers, if 'partition' is 0, return false. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return false.</param> | |
/// <param name="result">Result of the division.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Boolean TryDivision(this UInt32 number, UInt32 partition, out UInt32 result) | |
{ | |
if (partition != 0) | |
{ | |
result = number / partition; | |
return true; | |
} | |
result = 0; | |
return false; | |
} | |
/// <summary> | |
/// Attempt division between two numbers. If 'partition' is 0, return 0. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return 0.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static UInt32 SafeDivision(this UInt32 number, UInt32 partition) | |
{ | |
if (partition != 0) | |
{ | |
return number / partition; | |
} | |
return 0; | |
} | |
/// <summary> | |
/// Attempt division between two numbers, if 'partition' is 0, return false. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return false.</param> | |
/// <param name="result">Result of the division.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Boolean TryDivision(this Int64 number, Int64 partition, out Int64 result) | |
{ | |
if (partition != 0) | |
{ | |
result = number / partition; | |
return true; | |
} | |
result = 0; | |
return false; | |
} | |
/// <summary> | |
/// Attempt division between two numbers. If 'partition' is 0, return 0. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return 0.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Int64 SafeDivision(this Int64 number, Int64 partition) | |
{ | |
if (partition != 0) | |
{ | |
return number / partition; | |
} | |
return 0; | |
} | |
/// <summary> | |
/// Attempt division between two numbers, if 'partition' is 0, return false. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return false.</param> | |
/// <param name="result">Result of the division.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Boolean TryDivision(this UInt64 number, UInt64 partition, out UInt64 result) | |
{ | |
if (partition != 0) | |
{ | |
result = number / partition; | |
return true; | |
} | |
result = 0; | |
return false; | |
} | |
/// <summary> | |
/// Attempt division between two numbers. If 'partition' is 0, return 0. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return 0.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static UInt64 SafeDivision(this UInt64 number, UInt64 partition) | |
{ | |
if (partition != 0) | |
{ | |
return number / partition; | |
} | |
return 0; | |
} | |
/// <summary> | |
/// Attempt division between two numbers, if 'partition' is 0, return false. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return false.</param> | |
/// <param name="result">Result of the division.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Boolean TryDivision(this Single number, Single partition, out Single result) | |
{ | |
if (partition != 0) | |
{ | |
result = number / partition; | |
return true; | |
} | |
result = 0; | |
return false; | |
} | |
/// <summary> | |
/// Attempt division between two numbers. If 'partition' is 0, return 0. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return 0.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Single SafeDivision(this Single number, Single partition) | |
{ | |
if (partition != 0) | |
{ | |
return number / partition; | |
} | |
return 0; | |
} | |
/// <summary> | |
/// Attempt division between two numbers, if 'partition' is 0, return false. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return false.</param> | |
/// <param name="result">Result of the division.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Boolean TryDivision(this Double number, Double partition, out Double result) | |
{ | |
if (partition != 0) | |
{ | |
result = number / partition; | |
return true; | |
} | |
result = 0; | |
return false; | |
} | |
/// <summary> | |
/// Attempt division between two numbers. If 'partition' is 0, return 0. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return 0.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Double SafeDivision(this Double number, Double partition) | |
{ | |
if (partition != 0) | |
{ | |
return number / partition; | |
} | |
return 0; | |
} | |
/// <summary> | |
/// Attempt division between two numbers, if 'partition' is 0, return false. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return false.</param> | |
/// <param name="result">Result of the division.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Boolean TryDivision(this Decimal number, Decimal partition, out Decimal result) | |
{ | |
if (partition != 0) | |
{ | |
result = number / partition; | |
return true; | |
} | |
result = 0; | |
return false; | |
} | |
/// <summary> | |
/// Attempt division between two numbers. If 'partition' is 0, return 0. | |
/// </summary> | |
/// <param name="number">Number to divide from.</param> | |
/// <param name="partition">Number to divide with. If 0, return 0.</param> | |
/// <returns></returns> | |
[Pure] | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Decimal SafeDivision(this Decimal number, Decimal partition) | |
{ | |
if (partition != 0) | |
{ | |
return number / partition; | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment