Skip to content

Commit c189fe0

Browse files
committed
Added temperature units.
Added TemperatureAttribute. Added temperature units and modified UnitAttribute to use linear function instead of ratio. Added TemperatureTests.cs. Added invariant unit abbreviations for temperature units. Still missing russian. Failing test.
1 parent 0e73ab1 commit c189fe0

24 files changed

+1722
-407
lines changed

Src/UnitsNet/Attributes/Attributes.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class AngleAttribute : UnitAttribute
2929
public override Unit BaseUnit { get { return Unit.Degree; } }
3030

3131
public override string XmlDocSummary { get { return "In geometry, an angle is the figure formed by two rays, called the sides of the angle, sharing a common endpoint, called the vertex of the angle."; } }
32-
public AngleAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
32+
public AngleAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
3333
{
3434
}
3535
}
@@ -39,7 +39,7 @@ public class AreaAttribute : UnitAttribute
3939
{
4040
public override Unit BaseUnit { get { return Unit.SquareMeter; } }
4141
public override string XmlDocSummary { get { return "Area is a quantity that expresses the extent of a two-dimensional surface or shape, or planar lamina, in the plane. Area can be understood as the amount of material with a given thickness that would be necessary to fashion a model of the shape, or the amount of paint necessary to cover the surface with a single coat.[1] It is the two-dimensional analog of the length of a curve (a one-dimensional concept) or the volume of a solid (a three-dimensional concept)."; } }
42-
public AreaAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
42+
public AreaAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
4343
{
4444
}
4545
}
@@ -49,7 +49,7 @@ public class ElectricPotentialAttribute : UnitAttribute
4949
{
5050
public override Unit BaseUnit { get { return Unit.Volt; } }
5151
public override string XmlDocSummary { get { return "In classical electromagnetism, the electric potential (a scalar quantity denoted by Φ, ΦE or V and also called the electric field potential or the electrostatic potential) at a point is the amount of electric potential energy that a unitary point charge would have when located at that point."; } }
52-
public ElectricPotentialAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
52+
public ElectricPotentialAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
5353
{
5454
}
5555
}
@@ -59,7 +59,7 @@ public class FlowAttribute : UnitAttribute
5959
{
6060
public override Unit BaseUnit { get { return Unit.CubicMeterPerSecond; } }
6161
public override string XmlDocSummary { get { return "In physics and engineering, in particular fluid dynamics and hydrometry, the volumetric flow rate, (also known as volume flow rate, rate of fluid flow or volume velocity) is the volume of fluid which passes through a given surface per unit time. The SI unit is m3·s−1 (cubic meters per second). In US Customary Units and British Imperial Units, volumetric flow rate is often expressed as ft3/s (cubic feet per second). It is usually represented by the symbol Q."; } }
62-
public FlowAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
62+
public FlowAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
6363
{
6464
}
6565
}
@@ -69,7 +69,7 @@ public class ForceAttribute : UnitAttribute
6969
{
7070
public override Unit BaseUnit { get { return Unit.Newton; } }
7171
public override string XmlDocSummary { get { return "In physics, a force is any influence that causes an object to undergo a certain change, either concerning its movement, direction, or geometrical construction. In other words, a force can cause an object with mass to change its velocity (which includes to begin moving from a state of rest), i.e., to accelerate, or a flexible object to deform, or both. Force can also be described by intuitive concepts such as a push or a pull. A force has both magnitude and direction, making it a vector quantity. It is measured in the SI unit of newtons and represented by the symbol F."; } }
72-
public ForceAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
72+
public ForceAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
7373
{
7474
}
7575
}
@@ -79,7 +79,7 @@ public class LengthAttribute : UnitAttribute
7979
{
8080
public override Unit BaseUnit { get { return Unit.Meter; } }
8181
public override string XmlDocSummary { get { return "Many different units of length have been used around the world. The main units in modern use are U.S. customary units in the United States and the Metric system elsewhere. British Imperial units are still used for some purposes in the United Kingdom and some other countries. The metric system is sub-divided into SI and non-SI units."; } }
82-
public LengthAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
82+
public LengthAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
8383
{
8484
}
8585
}
@@ -89,7 +89,7 @@ public class MassAttribute : UnitAttribute
8989
{
9090
public override Unit BaseUnit { get { return Unit.Kilogram; } }
9191
public override string XmlDocSummary { get { return "In physics, mass (from Greek μᾶζα \"barley cake, lump [of dough]\") is a property of a physical system or body, giving rise to the phenomena of the body's resistance to being accelerated by a force and the strength of its mutual gravitational attraction with other bodies. Instruments such as mass balances or scales use those phenomena to measure mass. The SI unit of mass is the kilogram (kg)."; } }
92-
public MassAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
92+
public MassAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
9393
{
9494
}
9595
}
@@ -99,7 +99,7 @@ public class PressureAttribute : UnitAttribute
9999
{
100100
public override Unit BaseUnit { get { return Unit.Pascal; } }
101101
public override string XmlDocSummary { get { return "Pressure (symbol: P or p) is the ratio of force to the area over which that force is distributed. Pressure is force per unit area applied in a direction perpendicular to the surface of an object. Gauge pressure (also spelled gage pressure)[a] is the pressure relative to the local atmospheric or ambient pressure. Pressure is measured in any unit of force divided by any unit of area. The SI unit of pressure is the newton per square metre, which is called the pascal (Pa) after the seventeenth-century philosopher and scientist Blaise Pascal. A pressure of 1 Pa is small; it approximately equals the pressure exerted by a dollar bill resting flat on a table. Everyday pressures are often stated in kilopascals (1 kPa = 1000 Pa)."; } }
102-
public PressureAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
102+
public PressureAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
103103
{
104104
}
105105
}
@@ -109,8 +109,28 @@ public class RotationalSpeedAttribute : UnitAttribute
109109
{
110110
public override Unit BaseUnit { get { return Unit.RevolutionPerSecond; } }
111111
public override string XmlDocSummary { get { return "Rotational speed (sometimes called speed of revolution) is the number of complete rotations, revolutions, cycles, or turns per time unit. Rotational speed is a cyclic frequency, measured in radians per second or in hertz in the SI System by scientists, or in revolutions per minute (rpm or min-1) or revolutions per second in everyday life. The symbol for rotational speed is ω (the Greek lowercase letter \"omega\")."; } }
112-
public RotationalSpeedAttribute(double ratio, string pluralName = (string)null)
113-
: base(pluralName, ratio)
112+
public RotationalSpeedAttribute(double slope, string pluralName = (string)null)
113+
: base(pluralName, slope, offset: 0)
114+
{
115+
}
116+
}
117+
118+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
119+
public class TemperatureAttribute : UnitAttribute
120+
{
121+
public override Unit BaseUnit { get { return Unit.Kelvin; } }
122+
public override string XmlDocSummary { get { return "A temperature is a numerical measure of hot or cold. Its measurement is by detection of heat radiation or particle velocity or kinetic energy, or by the bulk behavior of a thermometric material. It may be calibrated in any of various temperature scales, Celsius, Fahrenheit, Kelvin, etc. The fundamental physical definition of temperature is provided by thermodynamics."; } }
123+
124+
/// <summary>
125+
/// Define a temperature unit by a linear mapping function to the base unit Kelvin.
126+
/// Example: Celsius is defined as C = K - 273.15. To define this as a function to the base unit,
127+
/// we invert it and get K = C + 273.15. From the function y = ax + b
128+
/// we find the slope 'a' as 1 and 'b' as 273.15.
129+
/// </summary>
130+
/// <param name="slope">Slope 'a' of function y = ax + b.</param>
131+
/// <param name="offset">Y-intercept 'b' of function y = ax + b.</param>
132+
/// <param name="pluralName">Name of unit in plural form. Used in methods and properties of unit class, such as FromMeters() method and Meters property in Length class.</param>
133+
public TemperatureAttribute(double slope, double offset = 0, string pluralName = (string)null) : base(pluralName, slope, offset)
114134
{
115135
}
116136
}
@@ -120,7 +140,7 @@ public class TorqueAttribute : UnitAttribute
120140
{
121141
public override Unit BaseUnit { get { return Unit.Newtonmeter; } }
122142
public override string XmlDocSummary { get { return "Torque, moment or moment of force (see the terminology below), is the tendency of a force to rotate an object about an axis,[1] fulcrum, or pivot. Just as a force is a push or a pull, a torque can be thought of as a twist to an object. Mathematically, torque is defined as the cross product of the lever-arm distance and force, which tends to produce rotation. Loosely speaking, torque is a measure of the turning force on an object such as a bolt or a flywheel. For example, pushing or pulling the handle of a wrench connected to a nut or bolt produces a torque (turning force) that loosens or tightens the nut or bolt."; } }
123-
public TorqueAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
143+
public TorqueAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
124144
{
125145
}
126146
}
@@ -130,7 +150,7 @@ public class VolumeAttribute : UnitAttribute
130150
{
131151
public override Unit BaseUnit { get { return Unit.CubicMeter; } }
132152
public override string XmlDocSummary { get { return "Volume is the quantity of three-dimensional space enclosed by some closed boundary, for example, the space that a substance (solid, liquid, gas, or plasma) or shape occupies or contains.[1] Volume is often quantified numerically using the SI derived unit, the cubic metre. The volume of a container is generally understood to be the capacity of the container, i. e. the amount of fluid (gas or liquid) that the container could hold, rather than the amount of space the container itself displaces."; } }
133-
public VolumeAttribute(double ratio, string pluralName = (string)null) : base(pluralName, ratio)
153+
public VolumeAttribute(double slope, string pluralName = (string)null) : base(pluralName, slope, offset: 0)
134154
{
135155
}
136156
}

Src/UnitsNet/Attributes/UnitAttribute.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
namespace UnitsNet.Attributes
44
{
5+
public struct LinearFunction
6+
{
7+
/// <summary>
8+
/// Slope of function, a, in y(x) = ax + b.
9+
/// </summary>
10+
// ReSharper disable once InconsistentNaming
11+
public readonly double a;
12+
13+
/// <summary>
14+
/// Y-intercept of function, b, in y(x) = ax + b.
15+
/// </summary>
16+
// ReSharper disable once InconsistentNaming
17+
public readonly double b;
18+
19+
/// <summary>
20+
/// Create linear function by its slope and y-intercept constants.
21+
/// </summary>
22+
/// <param name="a">Slope of function, a, in y(x) = ax + b.</param>
23+
/// <param name="b">Y-intercept of function, b, in y(x) = ax + b.</param>
24+
public LinearFunction(double a, double b)
25+
{
26+
this.b = b;
27+
this.a = a;
28+
}
29+
}
30+
531
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
632
public abstract class UnitAttribute : Attribute
733
{
@@ -21,16 +47,25 @@ public abstract class UnitAttribute : Attribute
2147
/// Ratio of unit to base unit. For example, <see cref="Unit.Kilometer" /> is 1000:1 of the base unit
2248
/// <see cref="Unit.Meter" />.
2349
/// </summary>
24-
public readonly double Ratio;
50+
public readonly LinearFunction LinearMappingFromBaseUnit;
2551

2652
/// <summary>
2753
/// XML doc summary for unit class. Will be inserted when generating the class from T4 template.
2854
/// </summary>
2955
public abstract string XmlDocSummary { get; }
3056

31-
public UnitAttribute(string pluralName, double ratio)
57+
public UnitAttribute(string pluralName, double slope, double offset)
3258
{
33-
Ratio = ratio;
59+
// Example: Kilometer has slope 1000, meaning for every kilometer the base unit increases with 1000 meters.
60+
// a: 1000
61+
// b: 0
62+
// y: base unit value in meters
63+
// x: unit value in kilometers
64+
// new Length(2000).Kilometers => (y - b) / a = (2000 - 0) / 1000 = 2km
65+
// Length.FromKilometers(2) => y = ax + b = 1000*2 + 0 = 2000m
66+
double a = slope;
67+
double b = offset;
68+
LinearMappingFromBaseUnit = new LinearFunction(a, b);
3469
PluralName = pluralName;
3570
}
3671
}

Src/UnitsNet/Generated Code/AngleUnit.g.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ public Angle(double degrees) : this()
4141

4242
#region Unit Properties
4343

44+
/// <summary>
45+
/// Get Angle in Gradians.
46+
/// </summary>
47+
/// <remarks>Example: x = (y - b) / a where x is value in Gradians and y is value in base unit Degrees.</remarks>
4448
public double Gradians
4549
{
46-
get { return Degrees/0.9; }
50+
get { return (Degrees - (0)) / 0.9; }
4751
}
4852

53+
/// <summary>
54+
/// Get Angle in Radians.
55+
/// </summary>
56+
/// <remarks>Example: x = (y - b) / a where x is value in Radians and y is value in base unit Degrees.</remarks>
4957
public double Radians
5058
{
51-
get { return Degrees/57.2957795130823; }
59+
get { return (Degrees - (0)) / 57.2957795130823; }
5260
}
5361

5462
#endregion
@@ -60,19 +68,31 @@ public static Angle Zero
6068
get { return new Angle(); }
6169
}
6270

71+
/// <summary>
72+
/// Get Angle from Degrees.
73+
/// </summary>
74+
/// <remarks>Example: y = ax + b where x is value in Degrees and y is value in base unit Degrees.</remarks>
6375
public static Angle FromDegrees(double degrees)
6476
{
65-
return new Angle(degrees*1);
77+
return new Angle(1 * degrees + 0);
6678
}
6779

80+
/// <summary>
81+
/// Get Angle from Gradians.
82+
/// </summary>
83+
/// <remarks>Example: y = ax + b where x is value in Gradians and y is value in base unit Degrees.</remarks>
6884
public static Angle FromGradians(double gradians)
6985
{
70-
return new Angle(gradians*0.9);
86+
return new Angle(0.9 * gradians + 0);
7187
}
7288

89+
/// <summary>
90+
/// Get Angle from Radians.
91+
/// </summary>
92+
/// <remarks>Example: y = ax + b where x is value in Radians and y is value in base unit Degrees.</remarks>
7393
public static Angle FromRadians(double radians)
7494
{
75-
return new Angle(radians*57.2957795130823);
95+
return new Angle(57.2957795130823 * radians + 0);
7696
}
7797

7898
#endregion

0 commit comments

Comments
 (0)