1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 00:25:40 +00:00

Add IsEven and IsOdd for integral types

This commit is contained in:
Oliver Booth 2019-12-16 17:55:55 +00:00
parent 9844bccd28
commit b2455d1bcd
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
6 changed files with 191 additions and 4 deletions

View File

@ -45,6 +45,28 @@
return BitConverter.GetBytes(number);
}
/// <summary>
/// Determines if the <see cref="Double"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this double number)
{
return Math.Abs(number % 2.0) < Double.Epsilon;
}
/// <summary>
/// Determines if the <see cref="Double"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this double number)
{
return !number.IsEven();
}
/// <summary>
/// Converts an angle from radians to degrees.
/// </summary>

View File

@ -138,6 +138,50 @@
return BitConverter.GetBytes(number);
}
/// <summary>
/// Determines if the <see cref="Int16"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this short number)
{
return ((long) number).IsEven();
}
/// <summary>
/// Determines if the <see cref="UInt16"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this ushort number)
{
return ((ulong) number).IsEven();
}
/// <summary>
/// Determines if the <see cref="Int16"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this short number)
{
return !number.IsEven();
}
/// <summary>
/// Determines if the <see cref="UInt16"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this ushort number)
{
return !number.IsEven();
}
/// <summary>
/// Determines if the <see cref="Int16"/> is a prime number.
/// </summary>
@ -149,6 +193,17 @@
return ((long) number).IsPrime();
}
/// <summary>
/// Determines if the <see cref="Int16"/> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns>
public static bool IsPrime(this ushort number)
{
return ((ulong) number).IsPrime();
}
/// <summary>
/// Gets an boolean value that represents this integer.
/// </summary>

View File

@ -298,6 +298,50 @@
return BitConverter.GetBytes(number);
}
/// <summary>
/// Determines if the <see cref="Int32"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this int number)
{
return ((long)number).IsEven();
}
/// <summary>
/// Determines if the <see cref="UInt32"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this uint number)
{
return ((ulong)number).IsEven();
}
/// <summary>
/// Determines if the <see cref="Int32"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this int number)
{
return !number.IsEven();
}
/// <summary>
/// Determines if the <see cref="UInt32"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this uint number)
{
return !number.IsEven();
}
/// <summary>
/// Determines if the <see cref="Int32"/> is a prime number.
/// </summary>

View File

@ -137,6 +137,50 @@
return BitConverter.GetBytes(number);
}
/// <summary>
/// Determines if the <see cref="Int64"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this long number)
{
return Math.Abs(number % 2.0) < Double.Epsilon;
}
/// <summary>
/// Determines if the <see cref="UInt64"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this ulong number)
{
return Math.Abs(number % 2.0) < Double.Epsilon;
}
/// <summary>
/// Determines if the <see cref="Int64"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this long number)
{
return !IsEven(number);
}
/// <summary>
/// Determines if the <see cref="UInt64"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this ulong number)
{
return !IsEven(number);
}
/// <summary>
/// Determines if the <see cref="Int64"/> is a prime number.
/// </summary>

View File

@ -15,12 +15,12 @@ Below is a list of the number of extension methods written for a given type. Ove
| `IConvertible` | `X10D` | 4 |
| `DateTime` | `X10D` | 11 |
| `EndPoint` | `X10D` | 2 |
| `double` | `X10D` | 5 |
| `double` | `X10D` | 7 |
| `enum` | `X10D` | 2 |
| `float` | `X10D` | 5 |
| `float` | `X10D` | 7 |
| `short` / `ushort` | `X10D` | 11 |
| `int` / `uint` | `X10D` | 23 |
| `long` / `ulong` | `X10D` | 11 |
| `int` / `uint` | `X10D` | 25 |
| `long` / `ulong` | `X10D` | 13 |
| `IList<T>` | `X10D` | 2 |
| `Random` | `X10D` | 2 |
| `string` / `SecureString` | `X10D` | 8 |

View File

@ -45,6 +45,28 @@
return BitConverter.GetBytes(number);
}
/// <summary>
/// Determines if the <see cref="Single"/> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
public static bool IsEven(this float number)
{
return ((double) number).IsEven();
}
/// <summary>
/// Determines if the <see cref="Single"/> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
public static bool IsOdd(this float number)
{
return !number.IsEven();
}
/// <summary>
/// Converts an angle from radians to degrees.
/// </summary>