Files
spark/src/client/spark_api/Co2/IntPrice.cs
T

59 lines
1.8 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace mbts.co2 {
[StructLayout(LayoutKind.Explicit)]
public readonly struct IntPrice : IEquatable<IntPrice> {
[FieldOffset(0)]
readonly long _mantissa;
[FieldOffset(7)]
readonly sbyte _exponent;
public IntPrice(long mantissa, sbyte exponent) {
_mantissa = mantissa;
_exponent = exponent;
}
public long mantissa => NativePriceHelpers.decode(_mantissa);
public sbyte exponent => _exponent;
public bool is_nan => _exponent == NativePriceHelpers.COCONUT_NAN_EXPONENT;
public double price() {
return NativePriceHelpers.to_double(mantissa, exponent);
}
public bool Equals(IntPrice other) {
return _exponent == other._exponent && _mantissa == other._mantissa;
}
public override bool Equals(object obj) {
return base.Equals(obj);
}
public static bool operator ==(IntPrice a, IntPrice b) {
return a.Equals(b);
}
public static bool operator !=(IntPrice a, IntPrice b) {
return !a.Equals(b);
}
public static bool operator < (IntPrice a, IntPrice b) {
if (a.exponent == b.exponent) {
return a.mantissa < b.mantissa;
}
return a.price() < b.price();
}
public static bool operator > (IntPrice a, IntPrice b) {
if (a.exponent == b.exponent) {
return a.mantissa > b.mantissa;
}
return a.price() > b.price();
}
public override string ToString() {
return $"{price():0.00}";
}
public override int GetHashCode() {
return _mantissa.GetHashCode();
}
public static IntPrice NaN = new IntPrice(0,127);
}
}