| | 1 | | namespace Towel.Mathematics; |
| | 2 | |
|
| | 3 | | /// <summary>Represents a rational value with a numerator and a denominator.</summary> |
| | 4 | | /// <typeparam name="T">The type of the numerator and denominator.</typeparam> |
| | 5 | | public struct Fraction<T> |
| | 6 | | { |
| | 7 | | internal T _numerator; |
| | 8 | | internal T _denominator; |
| | 9 | |
|
| | 10 | | #region Properties |
| | 11 | |
|
| | 12 | | /// <summary>The numerator of the fraction.</summary> |
| | 13 | | public T Numerator |
| | 14 | | { |
| 96 | 15 | | get => _numerator; |
| 0 | 16 | | set => _numerator = value; |
| | 17 | | } |
| | 18 | |
|
| | 19 | | /// <summary>The denominator of the fraction.</summary> |
| | 20 | | public T Denominator |
| | 21 | | { |
| 160 | 22 | | get => _denominator; |
| | 23 | | set |
| 0 | 24 | | { |
| 0 | 25 | | if (Equate(value, Constant<T>.Zero)) |
| 0 | 26 | | { |
| 0 | 27 | | throw new ArgumentOutOfRangeException(nameof(value), value, "!(" + nameof(value) + " != 0)"); |
| | 28 | | } |
| 0 | 29 | | _denominator = value; |
| 0 | 30 | | } |
| | 31 | | } |
| | 32 | |
|
| | 33 | | #endregion |
| | 34 | |
|
| | 35 | | #region Constructors |
| | 36 | |
|
| | 37 | | /// <summary>Constructs a new fraction [<paramref name="numerator"/> / 1].</summary> |
| | 38 | | /// <param name="numerator">The numerator of the fraction.</param> |
| | 39 | | public Fraction(T numerator) |
| 3 | 40 | | { |
| 3 | 41 | | _numerator = numerator; |
| 3 | 42 | | _denominator = Constant<T>.One; |
| 3 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary>Constructs a new fraction [<paramref name="numerator"/> / <paramref name="deniminator"/>].</summary> |
| | 46 | | /// <param name="numerator">The numerator of the fraction.</param> |
| | 47 | | /// <param name="deniminator">The denominator of the fraction.</param> |
| | 48 | | public Fraction(T numerator, T deniminator) |
| 207 | 49 | | { |
| 207 | 50 | | if (Equate(deniminator, Constant<T>.Zero)) |
| 0 | 51 | | { |
| 0 | 52 | | throw new ArgumentOutOfRangeException(nameof(deniminator), deniminator, nameof(deniminator) + " is 0"); |
| | 53 | | } |
| 207 | 54 | | ReduceInternal(numerator, deniminator, out T reducedNumerator, out T reducedDenominator); |
| 207 | 55 | | _numerator = reducedNumerator; |
| 207 | 56 | | _denominator = reducedDenominator; |
| 207 | 57 | | } |
| | 58 | |
|
| | 59 | | #endregion |
| | 60 | |
|
| | 61 | | #region Cast |
| | 62 | |
|
| | 63 | | /// <summary>Implicitly converts a value into a fraction.</summary> |
| | 64 | | /// <param name="value">The value to convert to a fraction.</param> |
| | 65 | | public static implicit operator Fraction<T>(T value) => |
| 0 | 66 | | new(value); |
| | 67 | |
|
| | 68 | | #endregion |
| | 69 | |
|
| | 70 | | #region Negation |
| | 71 | |
|
| | 72 | | /// <summary>Negates a value.</summary> |
| | 73 | | /// <param name="a">The fraction to negate.</param> |
| | 74 | | /// <returns>The result of the negation.</returns> |
| 0 | 75 | | public static Fraction<T> operator -(Fraction<T> a) => Fraction<T>.Negate(a); |
| | 76 | |
|
| | 77 | | /// <summary>Negates a value.</summary> |
| | 78 | | /// <param name="a">The value to negate.</param> |
| | 79 | | /// <returns>The result of the negation.</returns> |
| | 80 | | public static Fraction<T> Negate(Fraction<T> a) => |
| 0 | 81 | | new(Negation(a.Numerator), a.Denominator); |
| | 82 | |
|
| | 83 | | #endregion |
| | 84 | |
|
| | 85 | | #region Addition |
| | 86 | |
|
| | 87 | | /// <summary>Adds two values.</summary> |
| | 88 | | /// <param name="a">The left operand.</param> |
| | 89 | | /// <param name="b">The right operand.</param> |
| | 90 | | /// <returns>The result of the addition.</returns> |
| 20 | 91 | | public static Fraction<T> operator +(Fraction<T> a, Fraction<T> b) => Fraction<T>.Add(a, b); |
| | 92 | |
|
| | 93 | | /// <summary>Adds two values.</summary> |
| | 94 | | /// <param name="a">The left operand.</param> |
| | 95 | | /// <param name="b">The right operand.</param> |
| | 96 | | /// <returns>The result of the addition.</returns> |
| | 97 | | public static Fraction<T> Add(Fraction<T> a, Fraction<T> b) |
| 20 | 98 | | { |
| 20 | 99 | | T c = Multiplication(a.Numerator, b.Denominator); |
| 20 | 100 | | T d = Multiplication(b.Numerator, a.Denominator); |
| 20 | 101 | | T e = Addition(c, d); |
| 20 | 102 | | T f = Multiplication(a.Denominator, b.Denominator); |
| 20 | 103 | | return new Fraction<T>(e, f); |
| 20 | 104 | | } |
| | 105 | |
|
| | 106 | | #endregion |
| | 107 | |
|
| | 108 | | #region Subtraction |
| | 109 | |
|
| | 110 | | /// <summary>Subtracts two values.</summary> |
| | 111 | | /// <param name="a">The left operand.</param> |
| | 112 | | /// <param name="b">The right operand.</param> |
| | 113 | | /// <returns>The result of the subtraction.</returns> |
| | 114 | | public static Fraction<T> operator -(Fraction<T> a, Fraction<T> b) => |
| 12 | 115 | | Fraction<T>.Subtract(a, b); |
| | 116 | |
|
| | 117 | | /// <summary>Subtracts two values.</summary> |
| | 118 | | /// <param name="a">The left operand.</param> |
| | 119 | | /// <param name="b">The right operand.</param> |
| | 120 | | /// <returns>The result of the subtraction.</returns> |
| | 121 | | public static Fraction<T> Subtract(Fraction<T> a, Fraction<T> b) |
| 12 | 122 | | { |
| 12 | 123 | | T c = Multiplication(a.Numerator, b.Denominator); |
| 12 | 124 | | T d = Multiplication(b.Numerator, a.Denominator); |
| 12 | 125 | | T e = Subtraction(c, d); |
| 12 | 126 | | T f = Multiplication(a.Denominator, b.Denominator); |
| 12 | 127 | | return new Fraction<T>(e, f); |
| 12 | 128 | | } |
| | 129 | |
|
| | 130 | | #endregion |
| | 131 | |
|
| | 132 | | #region Multiplication |
| | 133 | |
|
| | 134 | | /// <summary>Multiplies two values.</summary> |
| | 135 | | /// <param name="a">The left operand.</param> |
| | 136 | | /// <param name="b">The right operand.</param> |
| | 137 | | /// <returns>The result of the multiplication.</returns> |
| | 138 | | public static Fraction<T> operator *(Fraction<T> a, Fraction<T> b) => |
| 16 | 139 | | Fraction<T>.Multiply(a, b); |
| | 140 | |
|
| | 141 | | /// <summary>Multiplies two values.</summary> |
| | 142 | | /// <param name="a">The left operand.</param> |
| | 143 | | /// <param name="b">The right operand.</param> |
| | 144 | | /// <returns>The result of the multiplication.</returns> |
| | 145 | | public static Fraction<T> Multiply(Fraction<T> a, Fraction<T> b) => |
| 16 | 146 | | new( |
| 16 | 147 | | Multiplication(a.Numerator, b.Numerator), |
| 16 | 148 | | Multiplication(a.Denominator, b.Denominator)); |
| | 149 | |
|
| | 150 | | #endregion |
| | 151 | |
|
| | 152 | | #region Division |
| | 153 | |
|
| | 154 | | /// <summary>Divides two values.</summary> |
| | 155 | | /// <param name="a">The left operand.</param> |
| | 156 | | /// <param name="b">The right operand.</param> |
| | 157 | | /// <returns>The result of the division.</returns> |
| | 158 | | public static Fraction<T> operator /(Fraction<T> a, Fraction<T> b) => |
| 0 | 159 | | Fraction<T>.Divide(a, b); |
| | 160 | |
|
| | 161 | | /// <summary>Divides two values.</summary> |
| | 162 | | /// <param name="a">The left operand.</param> |
| | 163 | | /// <param name="b">The right operand.</param> |
| | 164 | | /// <returns>The result of the division.</returns> |
| | 165 | | public static Fraction<T> Divide(Fraction<T> a, Fraction<T> b) => |
| 0 | 166 | | new( |
| 0 | 167 | | Multiplication(a.Numerator, b.Denominator), |
| 0 | 168 | | Multiplication(a.Denominator, b.Numerator)); |
| | 169 | |
|
| | 170 | | #endregion |
| | 171 | |
|
| | 172 | | #region Remainder |
| | 173 | |
|
| | 174 | | /// <summary>Computes the remainder of two values.</summary> |
| | 175 | | /// <param name="a">The left operand.</param> |
| | 176 | | /// <param name="b">The right operand.</param> |
| | 177 | | /// <returns>The result of the remainder operation.</returns> |
| | 178 | | public static Fraction<T> operator %(Fraction<T> a, Fraction<T> b) => |
| 0 | 179 | | Fraction<T>.Remainder(a, b); |
| | 180 | |
|
| | 181 | | /// <summary>Computes the remainder of two values.</summary> |
| | 182 | | /// <param name="a">The left operand.</param> |
| | 183 | | /// <param name="b">The right operand.</param> |
| | 184 | | /// <returns>The result of the remainder operation.</returns> |
| | 185 | | public static Fraction<T> Remainder(Fraction<T> a, Fraction<T> b) |
| 0 | 186 | | { |
| 0 | 187 | | while (a > b) |
| 0 | 188 | | { |
| 0 | 189 | | a -= b; |
| 0 | 190 | | } |
| 0 | 191 | | return a; |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | /// <summary>Computes the remainder of two values.</summary> |
| | 195 | | /// <param name="b">The right operand.</param> |
| | 196 | | /// <returns>The result of the remainder operation.</returns> |
| | 197 | | public Fraction<T> Remainder(Fraction<T> b) => |
| 0 | 198 | | Fraction<T>.Remainder(this, b); |
| | 199 | |
|
| | 200 | | #endregion |
| | 201 | |
|
| | 202 | | #region Reduce |
| | 203 | |
|
| | 204 | | internal static void ReduceInternal(T a, T b, out T c, out T d) |
| 207 | 205 | | { |
| 207 | 206 | | if (Statics.Equate(a, Constant<T>.Zero)) |
| 20 | 207 | | { |
| 20 | 208 | | c = a; |
| 20 | 209 | | d = Constant<T>.One; |
| 20 | 210 | | return; |
| | 211 | | } |
| 187 | 212 | | T gcf = GreatestCommonFactor(a, b); |
| 187 | 213 | | c = Division(a, gcf); |
| 187 | 214 | | d = Division(b, gcf); |
| 187 | 215 | | if (IsNegative(b)) |
| 0 | 216 | | { |
| 0 | 217 | | c = Multiplication(c, Constant<T>.NegativeOne); |
| 0 | 218 | | d = Multiplication(d, Constant<T>.NegativeOne); |
| 0 | 219 | | } |
| 207 | 220 | | } |
| | 221 | |
|
| | 222 | | /// <summary>Reduces a fractional value if possible.</summary> |
| | 223 | | /// <param name="a">The fractional value to reduce.</param> |
| | 224 | | /// <returns>The fraction in the reduced form.</returns> |
| | 225 | | public static Fraction<T> Reduce(Fraction<T> a) |
| 0 | 226 | | { |
| 0 | 227 | | ReduceInternal(a.Numerator, a.Denominator, out T numerator, out T denominator); |
| 0 | 228 | | return new Fraction<T>(numerator, denominator); |
| 0 | 229 | | } |
| | 230 | |
|
| | 231 | | /// <summary>Reduces a fractional value if possible.</summary> |
| | 232 | | /// <returns>The fraction in the reduced form.</returns> |
| | 233 | | public Fraction<T> Reduce() => |
| 0 | 234 | | Fraction<T>.Reduce(this); |
| | 235 | |
|
| | 236 | | #endregion |
| | 237 | |
|
| | 238 | | #region Equality |
| | 239 | |
|
| | 240 | | /// <summary>Checks for equality between two values.</summary> |
| | 241 | | /// <param name="a">The first operand.</param> |
| | 242 | | /// <param name="b">The second operand.</param> |
| | 243 | | /// <returns>The result of the equality check.</returns> |
| | 244 | | public static bool operator ==(Fraction<T> a, Fraction<T> b) => |
| 57 | 245 | | Fraction<T>.Equality(a, b); |
| | 246 | |
|
| | 247 | | /// <summary>Checks for equality between two values.</summary> |
| | 248 | | /// <param name="a">The first operand.</param> |
| | 249 | | /// <param name="b">The second operand.</param> |
| | 250 | | /// <returns>The result of the equality check.</returns> |
| | 251 | | public static bool Equality(Fraction<T> a, Fraction<T> b) => |
| 57 | 252 | | Equate(a._numerator, b._numerator) && |
| 57 | 253 | | Equate(a._denominator, b._denominator); |
| | 254 | |
|
| | 255 | | /// <summary>Checks for equality between two values.</summary> |
| | 256 | | /// <param name="b">The second operand.</param> |
| | 257 | | /// <returns>The result of the equality check.</returns> |
| | 258 | | public bool Equality(Fraction<T> b) => |
| 0 | 259 | | Fraction<T>.Equality(this, b); |
| | 260 | |
|
| | 261 | | /// <summary>Checks for equality with another object.</summary> |
| | 262 | | /// <param name="obj">The object to equate with this.</param> |
| | 263 | | /// <returns>The result of the equate.</returns> |
| | 264 | | public override bool Equals(object? obj) => |
| 0 | 265 | | obj is Fraction<T> fraction && Fraction<T>.Equality(this, fraction); |
| | 266 | |
|
| | 267 | | #endregion |
| | 268 | |
|
| | 269 | | #region Inequality |
| | 270 | |
|
| | 271 | | /// <summary>Checks for inequality between two values.</summary> |
| | 272 | | /// <param name="a">The first operand.</param> |
| | 273 | | /// <param name="b">The second operand.</param> |
| | 274 | | /// <returns>The result of the inequality check.</returns> |
| | 275 | | public static bool operator !=(Fraction<T> a, Fraction<T> b) => |
| 0 | 276 | | Fraction<T>.NotEqual(a, b); |
| | 277 | |
|
| | 278 | | /// <summary>Checks for inequality between two values.</summary> |
| | 279 | | /// <param name="a">The first operand.</param> |
| | 280 | | /// <param name="b">The second operand.</param> |
| | 281 | | /// <returns>The result of the inequality check.</returns> |
| | 282 | | public static bool NotEqual(Fraction<T> a, Fraction<T> b) => |
| 0 | 283 | | Inequate(a._numerator, b._numerator) || |
| 0 | 284 | | Inequate(a._denominator, b._denominator); |
| | 285 | |
|
| | 286 | | /// <summary>Checks for inequality between two values.</summary> |
| | 287 | | /// <param name="b">The second operand.</param> |
| | 288 | | /// <returns>The result of the inequality check.</returns> |
| | 289 | | public bool NotEqual(Fraction<T> b) => |
| 0 | 290 | | Fraction<T>.NotEqual(this, b); |
| | 291 | |
|
| | 292 | | #endregion |
| | 293 | |
|
| | 294 | | #region LessThan |
| | 295 | |
|
| | 296 | | /// <summary>Determines if one value is less than another.</summary> |
| | 297 | | /// <param name="a">The left operand.</param> |
| | 298 | | /// <param name="b">The right operand.</param> |
| | 299 | | /// <returns>The value of the less than operation.</returns> |
| | 300 | | public static bool operator <(Fraction<T> a, Fraction<T> b) => |
| 0 | 301 | | Fraction<T>.LessThan(a, b); |
| | 302 | |
|
| | 303 | | /// <summary>Determines if one value is less than another.</summary> |
| | 304 | | /// <param name="a">The left operand.</param> |
| | 305 | | /// <param name="b">The right operand.</param> |
| | 306 | | /// <returns>The value of the less than operation.</returns> |
| | 307 | | public static bool LessThan(Fraction<T> a, Fraction<T> b) |
| 0 | 308 | | { |
| 0 | 309 | | T c = Multiplication(a.Numerator, b.Denominator); |
| 0 | 310 | | T d = Multiplication(b.Numerator, a.Denominator); |
| 0 | 311 | | return Statics.LessThan(c, d); |
| 0 | 312 | | } |
| | 313 | |
|
| | 314 | | /// <summary>Determines if one value is less than another.</summary> |
| | 315 | | /// <param name="b">The right operand.</param> |
| | 316 | | /// <returns>The value of the less than operation.</returns> |
| | 317 | | public bool LessThan(Fraction<T> b) => |
| 0 | 318 | | Fraction<T>.LessThan(this, b); |
| | 319 | |
|
| | 320 | | #endregion |
| | 321 | |
|
| | 322 | | #region GreaterThan |
| | 323 | |
|
| | 324 | | /// <summary>Determines if one value is greater than another.</summary> |
| | 325 | | /// <param name="a">The left operand.</param> |
| | 326 | | /// <param name="b">The right operand.</param> |
| | 327 | | /// <returns>The value of the greater than operation.</returns> |
| | 328 | | public static bool operator >(Fraction<T> a, Fraction<T> b) => |
| 0 | 329 | | Fraction<T>.GreaterThan(a, b); |
| | 330 | |
|
| | 331 | | /// <summary>Determines if one value is greater than another.</summary> |
| | 332 | | /// <param name="a">The left operand.</param> |
| | 333 | | /// <param name="b">The right operand.</param> |
| | 334 | | /// <returns>The value of the greater than operation.</returns> |
| | 335 | | public static bool GreaterThan(Fraction<T> a, Fraction<T> b) |
| 0 | 336 | | { |
| 0 | 337 | | T c = Multiplication(a.Numerator, b.Denominator); |
| 0 | 338 | | T d = Multiplication(b.Numerator, a.Denominator); |
| 0 | 339 | | return Statics.GreaterThan(c, d); |
| 0 | 340 | | } |
| | 341 | |
|
| | 342 | | /// <summary>Determines if one value is greater than another.</summary> |
| | 343 | | /// <param name="b">The right operand.</param> |
| | 344 | | /// <returns>The value of the greater than operation.</returns> |
| | 345 | | public bool GreaterThan(Fraction<T> b) => |
| 0 | 346 | | Fraction<T>.GreaterThan(this, b); |
| | 347 | |
|
| | 348 | | #endregion |
| | 349 | |
|
| | 350 | | #region LessThanOrEqual |
| | 351 | |
|
| | 352 | | /// <summary>Determines if one value is less than another.</summary> |
| | 353 | | /// <param name="a">The left operand.</param> |
| | 354 | | /// <param name="b">The right operand.</param> |
| | 355 | | /// <returns>The value of the less than operation.</returns> |
| | 356 | | public static bool operator <=(Fraction<T> a, Fraction<T> b) => |
| 0 | 357 | | Fraction<T>.LessThanOrEqual(a, b); |
| | 358 | |
|
| | 359 | | /// <summary>Determines if one value is less than another.</summary> |
| | 360 | | /// <param name="a">The left operand.</param> |
| | 361 | | /// <param name="b">The right operand.</param> |
| | 362 | | /// <returns>The value of the less than operation.</returns> |
| | 363 | | public static bool LessThanOrEqual(Fraction<T> a, Fraction<T> b) |
| 0 | 364 | | { |
| 0 | 365 | | T c = Multiplication(a.Numerator, b.Denominator); |
| 0 | 366 | | T d = Multiplication(b.Numerator, a.Denominator); |
| 0 | 367 | | return Statics.LessThanOrEqual(c, d); |
| 0 | 368 | | } |
| | 369 | |
|
| | 370 | | /// <summary>Determines if one value is less than another.</summary> |
| | 371 | | /// <param name="b">The right operand.</param> |
| | 372 | | /// <returns>The value of the less than operation.</returns> |
| | 373 | | public bool LessThanOrEqual(Fraction<T> b) => |
| 0 | 374 | | Fraction<T>.LessThanOrEqual(this, b); |
| | 375 | |
|
| | 376 | | #endregion |
| | 377 | |
|
| | 378 | | #region GreaterThanOrEqual |
| | 379 | |
|
| | 380 | | /// <summary>Determines if one value is greater than another.</summary> |
| | 381 | | /// <param name="a">The left operand.</param> |
| | 382 | | /// <param name="b">The right operand.</param> |
| | 383 | | /// <returns>The value of the greater than operation.</returns> |
| | 384 | | public static bool operator >=(Fraction<T> a, Fraction<T> b) => |
| 0 | 385 | | Fraction<T>.GreaterThanOrEqual(a, b); |
| | 386 | |
|
| | 387 | | /// <summary>Determines if one value is greater than another.</summary> |
| | 388 | | /// <param name="a">The left operand.</param> |
| | 389 | | /// <param name="b">The right operand.</param> |
| | 390 | | /// <returns>The value of the greater than operation.</returns> |
| | 391 | | public static bool GreaterThanOrEqual(Fraction<T> a, Fraction<T> b) |
| 0 | 392 | | { |
| 0 | 393 | | T c = Multiplication(a.Numerator, b.Denominator); |
| 0 | 394 | | T d = Multiplication(b.Numerator, a.Denominator); |
| 0 | 395 | | return Statics.GreaterThanOrEqual(c, d); |
| 0 | 396 | | } |
| | 397 | |
|
| | 398 | | /// <summary>Determines if one value is greater than another.</summary> |
| | 399 | | /// <param name="b">The right operand.</param> |
| | 400 | | /// <returns>The value of the greater than operation.</returns> |
| | 401 | | public bool GreaterThanOrEqual(Fraction<T> b) => |
| 0 | 402 | | Fraction<T>.GreaterThanOrEqual(this, b); |
| | 403 | |
|
| | 404 | | #endregion |
| | 405 | |
|
| | 406 | | #region Compare |
| | 407 | |
|
| | 408 | | /// <summary>Compares two values.</summary> |
| | 409 | | /// <param name="b">The right operand.</param> |
| | 410 | | /// <returns>The result of the comparison.</returns> |
| | 411 | | public CompareResult Compare(Fraction<T> b) => |
| 0 | 412 | | Fraction<T>.Compare(this, b); |
| | 413 | |
|
| | 414 | | /// <summary>Compares two values.</summary> |
| | 415 | | /// <param name="a">The left operand.</param> |
| | 416 | | /// <param name="b">The right operand.</param> |
| | 417 | | /// <returns>The result of the comparison.</returns> |
| | 418 | | public static CompareResult Compare(Fraction<T> a, Fraction<T> b) => |
| 0 | 419 | | a < b ? Less : |
| 0 | 420 | | a > b ? Greater : |
| 0 | 421 | | Equal; |
| | 422 | |
|
| | 423 | | #endregion |
| | 424 | |
|
| | 425 | | #region Hash |
| | 426 | |
|
| | 427 | | /// <summary>Gets the default hash code for this instance.</summary> |
| | 428 | | /// <returns>Teh computed hash code.</returns> |
| 0 | 429 | | public override int GetHashCode() => HashCode.Combine(Hash(Numerator), Hash(Denominator)); |
| | 430 | |
|
| | 431 | | #endregion |
| | 432 | |
|
| | 433 | | #region TryParse + Parse |
| | 434 | |
|
| | 435 | | /// <summary>Tries to parse a <see cref="string"/> into a value of the type <see cref="Fraction{T}"/>.</summary> |
| | 436 | | /// <param name="string">The <see cref="string"/> to parse into a value ot type <see cref="Fraction{T}"/>.</param> |
| | 437 | | /// <param name="tryParse">The <see cref="Statics.TryParse{T}"/> method of the numerator and denomiator types.</param> |
| | 438 | | /// <returns> |
| | 439 | | /// - <see cref="bool"/> Success: True if the parse was successful; False if not.<br/> |
| | 440 | | /// - <see cref="Fraction{T}"/> Value: The value if the parse was successful or default if not. |
| | 441 | | /// </returns> |
| | 442 | | public static (bool Success, Fraction<T?> Value) TryParse(string @string, Func<string, (bool, T?)>? tryParse = null) = |
| 34 | 443 | | TryParse<SFunc<string, (bool, T?)>>(@string, tryParse ?? Statics.TryParse<T>); |
| | 444 | |
|
| | 445 | | /// <summary>Tries to parse a <see cref="string"/> into a value of the type <see cref="Fraction{T}"/>.</summary> |
| | 446 | | /// <typeparam name="TryParse">The type of TryParse method of the numerator and denomiator types.</typeparam> |
| | 447 | | /// <param name="string">The <see cref="string"/> to parse into a value ot type <see cref="Fraction{T}"/>.</param> |
| | 448 | | /// <param name="tryParse">The TryParse method of the numerator and denomiator types.</param> |
| | 449 | | /// <returns> |
| | 450 | | /// - <see cref="bool"/> Success: True if the parse was successful; False if not.<br/> |
| | 451 | | /// - <see cref="Fraction{T}"/> Value: The value if the parse was successful or default if not. |
| | 452 | | /// </returns> |
| | 453 | | public static (bool Success, Fraction<T?> Value) TryParse<TryParse>(string @string, TryParse tryParse = default) |
| | 454 | | where TryParse : struct, IFunc<string, (bool Success, T? Value)> |
| 34 | 455 | | { |
| 34 | 456 | | bool containsWhiteSpace = false; |
| 320 | 457 | | foreach (char c in @string) |
| 109 | 458 | | { |
| 109 | 459 | | containsWhiteSpace = containsWhiteSpace || char.IsWhiteSpace(c); |
| 109 | 460 | | } |
| 34 | 461 | | if (!containsWhiteSpace) |
| 17 | 462 | | { |
| 17 | 463 | | int divideIndex = @string.IndexOf("/"); |
| 17 | 464 | | if (divideIndex >= 0) |
| 10 | 465 | | { |
| 10 | 466 | | string numeratorString = @string[..divideIndex]; |
| 10 | 467 | | string denominatorString = @string[(divideIndex + 1)..]; |
| 10 | 468 | | if (denominatorString is not "0") |
| 9 | 469 | | { |
| 9 | 470 | | var (numeratorSuccess, numerator) = tryParse.Invoke(numeratorString); |
| 9 | 471 | | if (numeratorSuccess) |
| 7 | 472 | | { |
| 7 | 473 | | var (denominatorSuccess, denominator) = tryParse.Invoke(denominatorString); |
| 7 | 474 | | if (denominatorSuccess && !Equate(denominator, Constant<T>.Zero)) |
| 6 | 475 | | { |
| 6 | 476 | | return (true, new Fraction<T?>(numerator, denominator)); |
| | 477 | | } |
| 1 | 478 | | } |
| 3 | 479 | | } |
| 4 | 480 | | } |
| | 481 | | else |
| 7 | 482 | | { |
| 7 | 483 | | var (success, value) = tryParse.Invoke(@string); |
| 7 | 484 | | if (success) |
| 3 | 485 | | { |
| 3 | 486 | | return (true, new Fraction<T?>(value)); |
| | 487 | | } |
| 4 | 488 | | } |
| 8 | 489 | | } |
| 25 | 490 | | return (false, default); |
| 34 | 491 | | } |
| | 492 | |
|
| | 493 | | /// <summary>Parses a string into a fraction.</summary> |
| | 494 | | /// <param name="string">The string to parse.</param> |
| | 495 | | /// <returns>The parsed value from the string.</returns> |
| | 496 | | public static Fraction<T?> Parse(string @string) |
| 0 | 497 | | { |
| 0 | 498 | | var (success, value) = TryParse(@string); |
| 0 | 499 | | if (success) |
| 0 | 500 | | { |
| 0 | 501 | | return value; |
| | 502 | | } |
| | 503 | | else |
| 0 | 504 | | { |
| 0 | 505 | | throw new ArgumentException($@"the {nameof(@string)} parameter was not in a parsable format", nameof(@string)); |
| | 506 | | } |
| 0 | 507 | | } |
| | 508 | |
|
| | 509 | | #endregion |
| | 510 | |
|
| | 511 | | #region ToString |
| | 512 | |
|
| | 513 | | /// <summary>Default conversion to string for fractions.</summary> |
| | 514 | | /// <returns>The value represented as a string.</returns> |
| 0 | 515 | | public override string? ToString() => ToString(this); |
| | 516 | |
|
| | 517 | | /// <summary>Default conversion to string for fractions.</summary> |
| | 518 | | /// <param name="fraction">The value to convert.</param> |
| | 519 | | /// <param name="toString">The string conversion function for the numerator and denominator.</param> |
| | 520 | | /// <returns>The value represented as a string.</returns> |
| | 521 | | public static string? ToString(Fraction<T> fraction, Func<T, string?>? toString = null) => |
| 0 | 522 | | ToString<SFunc<T, string?>>(fraction, toString ?? (value => value is null ? string.Empty : value.ToString())); |
| | 523 | |
|
| | 524 | | /// <summary>Default conversion to string for fractions.</summary> |
| | 525 | | /// <typeparam name="TToString">The type of the string conversion function for the numerator and denominator.</typepar |
| | 526 | | /// <param name="fraction">The value to convert.</param> |
| | 527 | | /// <param name="toString">The string conversion function for the numerator and denominator.</param> |
| | 528 | | /// <returns>The value represented as a string.</returns> |
| | 529 | | public static string? ToString<TToString>(Fraction<T> fraction, TToString toString = default) |
| | 530 | | where TToString : struct, IFunc<T, string?> |
| 0 | 531 | | { |
| 0 | 532 | | if (Equate(fraction._denominator, Constant<T>.One)) |
| 0 | 533 | | { |
| 0 | 534 | | return toString.Invoke(fraction._numerator); |
| | 535 | | } |
| 0 | 536 | | return toString.Invoke(fraction._numerator) + "/" + toString.Invoke(fraction._denominator); |
| 0 | 537 | | } |
| | 538 | |
|
| | 539 | | #endregion |
| | 540 | | } |