1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
|
// ==========================================================
// FreeImage 3 .NET wrapper
// Original FreeImage 3 functions and .NET compatible derived functions
//
// Design and implementation by
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
// - Carsten Klein (cklein05@users.sourceforge.net)
//
// Contributors:
// - David Boland (davidboland@vodafone.ie)
//
// Main reference : MSDN Knowlede Base
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
// ==========================================================
// CVS
// $Revision: 1.5 $
// $Date: 2009/02/27 16:36:23 $
// $Id: FIURational.cs,v 1.5 2009/02/27 16:36:23 cklein05 Exp $
// ==========================================================
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace FreeImageAPI
{
/// <summary>
/// The <b>FIURational</b> structure represents a fraction via two <see cref="UInt32"/>
/// instances which are interpreted as numerator and denominator.
/// </summary>
/// <remarks>
/// The structure tries to approximate the value of <see cref="FreeImageAPI.FIURational(decimal)"/>
/// when creating a new instance by using a better algorithm than FreeImage does.
/// <para/>
/// The structure implements the following operators:
/// +, ++, --, ==, != , >, >==, <, <== and ~ (which switches nominator and denomiator).
/// <para/>
/// The structure can be converted into all .NET standard types either implicit or
/// explicit.
/// </remarks>
[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct FIURational : IConvertible, IComparable, IFormattable, IComparable<FIURational>, IEquatable<FIURational>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private uint numerator;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private uint denominator;
/// <summary>
/// Represents the largest possible value of <see cref="FIURational"/>. This field is constant.
/// </summary>
public static readonly FIURational MaxValue = new FIURational(UInt32.MaxValue, 1u);
/// <summary>
/// Represents the smallest possible value of <see cref="FIURational"/>. This field is constant.
/// </summary>
public static readonly FIURational MinValue = new FIURational(0u, 1u);
/// <summary>
/// Represents the smallest positive <see cref="FIURational"/> value greater than zero. This field is constant.
/// </summary>
public static readonly FIURational Epsilon = new FIURational(1u, UInt32.MaxValue);
/// <summary>
/// Initializes a new instance based on the specified parameters.
/// </summary>
/// <param name="n">The numerator.</param>
/// <param name="d">The denominator.</param>
public FIURational(uint n, uint d)
{
numerator = n;
denominator = d;
Normalize();
}
/// <summary>
/// Initializes a new instance based on the specified parameters.
/// </summary>
/// <param name="tag">The tag to read the data from.</param>
public unsafe FIURational(FITAG tag)
{
switch (FreeImage.GetTagType(tag))
{
case FREE_IMAGE_MDTYPE.FIDT_RATIONAL:
uint* pvalue = (uint*)FreeImage.GetTagValue(tag);
numerator = pvalue[0];
denominator = pvalue[1];
Normalize();
return;
default:
throw new ArgumentException("tag");
}
}
/// <summary>
///Initializes a new instance based on the specified parameters.
/// </summary>
/// <param name="value">The value to convert into a fraction.</param>
/// <exception cref="OverflowException">
/// <paramref name="value"/> cannot be converted into a fraction
/// represented by two unsigned integer values.</exception>
public FIURational(decimal value)
{
try
{
if (value < 0)
{
throw new OverflowException("value");
}
try
{
int[] contFract = CreateContinuedFraction(value);
CreateFraction(contFract, out numerator, out denominator);
Normalize();
}
catch
{
numerator = 0;
denominator = 1;
}
if (Math.Abs(((decimal)numerator / (decimal)denominator) - value) > 0.0001m)
{
int maxDen = (Int32.MaxValue / (int)value) - 2;
maxDen = maxDen < 10000 ? maxDen : 10000;
ApproximateFraction(value, maxDen, out numerator, out denominator);
Normalize();
if (Math.Abs(((decimal)numerator / (decimal)denominator) - value) > 0.0001m)
{
throw new OverflowException("Unable to convert value into a fraction");
}
}
Normalize();
}
catch (Exception ex)
{
throw new OverflowException("Unable to calculate fraction.", ex);
}
}
/// <summary>
/// The numerator of the fraction.
/// </summary>
public uint Numerator
{
get { return numerator; }
}
/// <summary>
/// The denominator of the fraction.
/// </summary>
public uint Denominator
{
get { return denominator; }
}
/// <summary>
/// Returns the truncated value of the fraction.
/// </summary>
/// <returns></returns>
public int Truncate()
{
return denominator > 0 ? (int)(numerator / denominator) : 0;
}
/// <summary>
/// Returns whether the fraction is representing an integer value.
/// </summary>
public bool IsInteger
{
get
{
return (denominator == 1 ||
(denominator != 0 && (numerator % denominator == 0)) ||
(denominator == 0 && numerator == 0));
}
}
/// <summary>
/// Calculated the greatest common divisor of 'a' and 'b'.
/// </summary>
private static ulong Gcd(ulong a, ulong b)
{
ulong r;
while (b > 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
/// <summary>
/// Calculated the smallest common multiple of 'a' and 'b'.
/// </summary>
private static ulong Scm(uint n, uint m)
{
return (ulong)n * (ulong)m / Gcd(n, m);
}
/// <summary>
/// Normalizes the fraction.
/// </summary>
private void Normalize()
{
if (denominator == 0)
{
numerator = 0;
denominator = 1;
return;
}
if (numerator != 1 && denominator != 1)
{
uint common = (uint)Gcd(numerator, denominator);
if (common != 1 && common != 0)
{
numerator /= common;
denominator /= common;
}
}
}
/// <summary>
/// Normalizes a fraction.
/// </summary>
private static void Normalize(ref ulong numerator, ref ulong denominator)
{
if (denominator == 0)
{
numerator = 0;
denominator = 1;
}
else if (numerator != 1 && denominator != 1)
{
ulong common = Gcd(numerator, denominator);
if (common != 1)
{
numerator /= common;
denominator /= common;
}
}
}
/// <summary>
/// Returns the digits after the point.
/// </summary>
private static int GetDigits(decimal value)
{
int result = 0;
value -= decimal.Truncate(value);
while (value != 0)
{
value *= 10;
value -= decimal.Truncate(value);
result++;
}
return result;
}
/// <summary>
/// Creates a continued fraction of a decimal value.
/// </summary>
private static int[] CreateContinuedFraction(decimal value)
{
int precision = GetDigits(value);
decimal epsilon = 0.0000001m;
List<int> list = new List<int>();
value = Math.Abs(value);
byte b = 0;
list.Add((int)value);
value -= ((int)value);
while (value != 0m)
{
if (++b == byte.MaxValue || value < epsilon)
{
break;
}
value = 1m / value;
if (Math.Abs((Math.Round(value, precision - 1) - value)) < epsilon)
{
value = Math.Round(value, precision - 1);
}
list.Add((int)value);
value -= ((int)value);
}
return list.ToArray();
}
/// <summary>
/// Creates a fraction from a continued fraction.
/// </summary>
private static void CreateFraction(int[] continuedFraction, out uint numerator, out uint denominator)
{
numerator = 1;
denominator = 0;
uint temp;
for (int i = continuedFraction.Length - 1; i > -1; i--)
{
temp = numerator;
numerator = (uint)(continuedFraction[i] * numerator + denominator);
denominator = temp;
}
}
/// <summary>
/// Tries 'brute force' to approximate <paramref name="value"/> with a fraction.
/// </summary>
private static void ApproximateFraction(decimal value, int maxDen, out uint num, out uint den)
{
num = 0;
den = 0;
decimal bestDifference = 1m;
decimal currentDifference = -1m;
int digits = GetDigits(value);
if (digits <= 9)
{
uint mul = 1;
for (int i = 1; i <= digits; i++)
{
mul *= 10;
}
if (mul <= maxDen)
{
num = (uint)(value * mul);
den = mul;
return;
}
}
for (uint u = 1; u <= maxDen; u++)
{
uint numerator = (uint)Math.Floor(value * (decimal)u + 0.5m);
currentDifference = Math.Abs(value - (decimal)numerator / (decimal)u);
if (currentDifference < bestDifference)
{
num = numerator;
den = u;
bestDifference = currentDifference;
}
}
}
/// <summary>
/// Converts the numeric value of the <see cref="FIURational"/> object
/// to its equivalent string representation.
/// </summary>
/// <returns>The string representation of the value of this instance.</returns>
public override string ToString()
{
return ((IConvertible)this).ToDouble(null).ToString();
}
/// <summary>
/// Tests whether the specified object is a <see cref="FIURational"/> structure
/// and is equivalent to this <see cref="FIURational"/> structure.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="FIURational"/> structure
/// equivalent to this <see cref="FIURational"/> structure; otherwise, <b>false</b>.</returns>
public override bool Equals(object obj)
{
return ((obj is FIURational) && (this == ((FIURational)obj)));
}
/// <summary>
/// Returns a hash code for this <see cref="FIURational"/> structure.
/// </summary>
/// <returns>An integer value that specifies the hash code for this <see cref="FIURational"/>.</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
#region Operators
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator +(FIURational value)
{
return value;
}
/// <summary>
/// Returns the reciprocal value of this instance.
/// </summary>
public static FIURational operator ~(FIURational value)
{
uint temp = value.denominator;
value.denominator = value.numerator;
value.numerator = temp;
value.Normalize();
return value;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator ++(FIURational value)
{
checked
{
value.numerator += value.denominator;
}
return value;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator --(FIURational value)
{
checked
{
value.numerator -= value.denominator;
}
return value;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator +(FIURational left, FIURational right)
{
ulong numerator = 0;
ulong denominator = Scm(left.denominator, right.denominator);
numerator = (left.numerator * (denominator / left.denominator)) +
(right.numerator * (denominator / right.denominator));
Normalize(ref numerator, ref denominator);
checked
{
return new FIURational((uint)numerator, (uint)denominator);
}
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator -(FIURational left, FIURational right)
{
checked
{
if (left.denominator != right.denominator)
{
uint denom = left.denominator;
left.numerator *= right.denominator;
left.denominator *= right.denominator;
right.numerator *= denom;
right.denominator *= denom;
}
left.numerator -= right.numerator;
left.Normalize();
return left;
}
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator *(FIURational left, FIURational r2)
{
ulong numerator = left.numerator * r2.numerator;
ulong denominator = left.denominator * r2.denominator;
Normalize(ref numerator, ref denominator);
checked
{
return new FIURational((uint)numerator, (uint)denominator);
}
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator /(FIURational left, FIURational right)
{
uint temp = right.denominator;
right.denominator = right.numerator;
right.numerator = temp;
return left * right;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIURational operator %(FIURational left, FIURational right)
{
right.Normalize();
if (Math.Abs(right.numerator) < right.denominator)
return new FIURational(0, 0);
int div = (int)(left / right);
return left - (right * div);
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator ==(FIURational left, FIURational right)
{
left.Normalize();
right.Normalize();
return (left.numerator == right.numerator) && (left.denominator == right.denominator);
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator !=(FIURational left, FIURational right)
{
left.Normalize();
right.Normalize();
return (left.numerator != right.numerator) || (left.denominator != right.denominator);
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator >(FIURational left, FIURational right)
{
ulong denominator = Scm(left.denominator, right.denominator);
return (left.numerator * (denominator / left.denominator)) >
(right.numerator * (denominator / right.denominator));
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator <(FIURational left, FIURational right)
{
ulong denominator = Scm(left.denominator, right.denominator);
return (left.numerator * (denominator / left.denominator)) <
(right.numerator * (denominator / right.denominator));
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator >=(FIURational left, FIURational right)
{
ulong denominator = Scm(left.denominator, right.denominator);
return (left.numerator * (denominator / left.denominator)) >=
(right.numerator * (denominator / right.denominator));
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator <=(FIURational left, FIURational right)
{
ulong denominator = Scm(left.denominator, right.denominator);
return (left.numerator * (denominator / left.denominator)) <=
(right.numerator * (denominator / right.denominator));
}
#endregion
#region Conversions
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Boolean"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Boolean"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator bool(FIURational value)
{
return (value.numerator != 0);
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Byte"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Byte"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator byte(FIURational value)
{
return (byte)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Char"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Char"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator char(FIURational value)
{
return (char)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Decimal"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Decimal"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator decimal(FIURational value)
{
return value.denominator == 0 ? 0m : (decimal)value.numerator / (decimal)value.denominator;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Double"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Double"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator double(FIURational value)
{
return value.denominator == 0 ? 0d : (double)value.numerator / (double)value.denominator;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="Int16"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Int16"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator short(FIURational value)
{
return (short)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="Int32"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Int32"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator int(FIURational value)
{
return (int)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="Int64"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Int64"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator long(FIURational value)
{
return (byte)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Single"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="Single"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator float(FIURational value)
{
return value.denominator == 0 ? 0f : (float)value.numerator / (float)value.denominator;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="SByte"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="SByte"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator sbyte(FIURational value)
{
return (sbyte)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="UInt16"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="UInt16"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator ushort(FIURational value)
{
return (ushort)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="UInt32"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="UInt32"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator uint(FIURational value)
{
return (uint)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="UInt32"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIURational"/> structure.</param>
/// <returns>A new instance of <see cref="UInt32"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator ulong(FIURational value)
{
return (ulong)(double)value;
}
//
/// <summary>
/// Converts the value of a <see cref="Boolean"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">A <see cref="Boolean"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator FIURational(bool value)
{
return new FIURational(value ? 1u : 0u, 1u);
}
/// <summary>
/// Converts the value of a <see cref="Byte"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">A <see cref="Byte"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator FIURational(byte value)
{
return new FIURational(value, 1);
}
/// <summary>
/// Converts the value of a <see cref="Char"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">A <see cref="Char"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator FIURational(char value)
{
return new FIURational(value, 1);
}
/// <summary>
/// Converts the value of a <see cref="Decimal"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">A <see cref="Decimal"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator FIURational(decimal value)
{
return new FIURational(value);
}
/// <summary>
/// Converts the value of a <see cref="Double"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">A <see cref="Double"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator FIURational(double value)
{
return new FIURational((decimal)value);
}
/// <summary>
/// Converts the value of an <see cref="Int16"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">An <see cref="Int16"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator FIURational(short value)
{
return new FIURational((uint)value, 1u);
}
/// <summary>
/// Converts the value of an <see cref="Int32"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">An <see cref="Int32"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator FIURational(int value)
{
return new FIURational((uint)value, 1u);
}
/// <summary>
/// Converts the value of an <see cref="Int64"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">An <see cref="Int64"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator FIURational(long value)
{
return new FIURational((uint)value, 1u);
}
/// <summary>
/// Converts the value of a <see cref="SByte"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">A <see cref="SByte"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator FIURational(sbyte value)
{
return new FIURational((uint)value, 1u);
}
/// <summary>
/// Converts the value of a <see cref="Single"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">A <see cref="Single"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator FIURational(float value)
{
return new FIURational((decimal)value);
}
/// <summary>
/// Converts the value of an <see cref="UInt16"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">An <see cref="UInt16"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator FIURational(ushort value)
{
return new FIURational(value, 1);
}
/// <summary>
/// Converts the value of an <see cref="UInt32"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">An <see cref="UInt32"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator FIURational(uint value)
{
return new FIURational(value, 1u);
}
/// <summary>
/// Converts the value of an <see cref="UInt64"/> structure to a <see cref="FIURational"/> structure.
/// </summary>
/// <param name="value">An <see cref="UInt64"/> structure.</param>
/// <returns>A new instance of <see cref="FIURational"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator FIURational(ulong value)
{
return new FIURational((uint)value, 1u);
}
#endregion
#region IConvertible Member
TypeCode IConvertible.GetTypeCode()
{
return TypeCode.Double;
}
bool IConvertible.ToBoolean(IFormatProvider provider)
{
return (bool)this;
}
byte IConvertible.ToByte(IFormatProvider provider)
{
return (byte)this;
}
char IConvertible.ToChar(IFormatProvider provider)
{
return (char)this;
}
DateTime IConvertible.ToDateTime(IFormatProvider provider)
{
return Convert.ToDateTime(((IConvertible)this).ToDouble(provider));
}
decimal IConvertible.ToDecimal(IFormatProvider provider)
{
return this;
}
double IConvertible.ToDouble(IFormatProvider provider)
{
return this;
}
short IConvertible.ToInt16(IFormatProvider provider)
{
return (short)this;
}
int IConvertible.ToInt32(IFormatProvider provider)
{
return (int)this;
}
long IConvertible.ToInt64(IFormatProvider provider)
{
return (long)this;
}
sbyte IConvertible.ToSByte(IFormatProvider provider)
{
return (sbyte)this;
}
float IConvertible.ToSingle(IFormatProvider provider)
{
return this;
}
string IConvertible.ToString(IFormatProvider provider)
{
return ToString(((double)this).ToString(), provider);
}
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
return Convert.ChangeType(((IConvertible)this).ToDouble(provider), conversionType, provider);
}
ushort IConvertible.ToUInt16(IFormatProvider provider)
{
return (ushort)this;
}
uint IConvertible.ToUInt32(IFormatProvider provider)
{
return (uint)this;
}
ulong IConvertible.ToUInt64(IFormatProvider provider)
{
return (ulong)this;
}
#endregion
#region IComparable Member
/// <summary>
/// Compares this instance with a specified <see cref="Object"/>.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A 32-bit signed integer indicating the lexical relationship between the two comparands.</returns>
/// <exception cref="ArgumentException"><paramref name="obj"/> is not a <see cref="FIURational"/>.</exception>
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
if (!(obj is FIURational))
{
throw new ArgumentException();
}
return CompareTo((FIURational)obj);
}
#endregion
#region IFormattable Member
/// <summary>
/// Formats the value of the current instance using the specified format.
/// </summary>
/// <param name="format">The String specifying the format to use.</param>
/// <param name="formatProvider">The IFormatProvider to use to format the value.</param>
/// <returns>A String containing the value of the current instance in the specified format.</returns>
public string ToString(string format, IFormatProvider formatProvider)
{
if (format == null)
{
format = "";
}
return String.Format(formatProvider, format, ((IConvertible)this).ToDouble(formatProvider));
}
#endregion
#region IEquatable<FIURational> Member
/// <summary>
/// Tests whether the specified <see cref="FIURational"/> structure is equivalent to this <see cref="FIURational"/> structure.
/// </summary>
/// <param name="other">A <see cref="FIURational"/> structure to compare to this instance.</param>
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="FIURational"/> structure
/// equivalent to this <see cref="FIURational"/> structure; otherwise, <b>false</b>.</returns>
public bool Equals(FIURational other)
{
return (this == other);
}
#endregion
#region IComparable<FIURational> Member
/// <summary>
/// Compares this instance with a specified <see cref="FIURational"/> object.
/// </summary>
/// <param name="other">A <see cref="FIURational"/> to compare.</param>
/// <returns>A signed number indicating the relative values of this instance
/// and <paramref name="other"/>.</returns>
public int CompareTo(FIURational other)
{
FIURational difference = this - other;
difference.Normalize();
if (difference.numerator > 0) return 1;
if (difference.numerator < 0) return -1;
else return 0;
}
#endregion
}
}
|