(C#)分数的重载运算
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 分数的加减乘除运算符重载
///
public class Rfloat
{
private float fNumerator = 0f;
private float fDenominator = 0f;
public Rfloat(float numerator, float denominator)
{
fNumerator = numerator;
fDenominator = denominator;
}
public static Rfloat operator +(Rfloat a, Rfloat b)
{
var denominator = a.fDenominator * b.fDenominator;
var numerator = a.fNumerator * b.fDenominator + b.fNumerator * a.fDenominator;
return new Rfloat(numerator, denominator);
}
public static Rfloat operator -(Rfloat a, Rfloat b)
{
var denominator = a.fDenominator * b.fDenominator;
var numerator = a.fNumerator * b.fDenominator - b.fNumerator * a.fDenominator;
return new Rfloat(numerator, denominator);
}
public static Rfloat operator *(Rfloat a, Rfloat b)
{
var denominator = a.fDenominator * b.fDenominator;
var numerator = a.fNumerator * b.fNumerator;
return new Rfloat(numerator, denominator);
}
public static Rfloat operator /(Rfloat a, Rfloat b)
{
var denominator = a.fDenominator * b.fNumerator;
var numerator = a.fNumerator * b.fDenominator;
return new Rfloat(numerator, denominator);
}
public float GetFloat()
{
return fNumerator / fDenominator;
}
}