Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DoubleNumber.cs
1using System;
2using System.Reflection;
5
7{
11 public sealed class DoubleNumber : FieldElement
12 {
16 public static readonly DoubleNumber ZeroElement = new DoubleNumber(0);
17
21 public static readonly DoubleNumber OneElement = new DoubleNumber(1);
22
26 public static readonly DoubleNumber TwoElement = new DoubleNumber(2);
27
31 public static readonly DoubleNumber ThreeElement = new DoubleNumber(3);
32
33 private double value;
34
39 public DoubleNumber(double Value)
40 {
41 this.value = Value;
42 }
43
47 public double Value
48 {
49 get => this.value;
50 set => this.value = value;
51 }
52
54 public override string ToString()
55 {
56 return Expression.ToString(this.value);
57 }
58
62 public override IField AssociatedField
63 {
64 get { return DoubleNumbers.Instance; }
65 }
66
70 public override object AssociatedObjectValue => this.value;
71
78 {
79 if (Element.AssociatedObjectValue is double d)
80 return new DoubleNumber(this.value * d);
81 else
82 return null;
83 }
84
89 public override IRingElement Invert()
90 {
91 return new DoubleNumber(1.0 / this.value);
92 }
93
100 {
101 if (Element.AssociatedObjectValue is double d)
102 return new DoubleNumber(this.value + d);
103 else
104 return null;
105 }
106
111 public override IGroupElement Negate()
112 {
113 return new DoubleNumber(-this.value);
114 }
115
117 public override bool Equals(object obj)
118 {
119 if (!(obj is IElement E))
120 return false;
121
122 if (E.AssociatedObjectValue is double d)
123 return this.value == d;
124 else
125 return false;
126 }
127
129 public override int GetHashCode()
130 {
131 return this.value.GetHashCode();
132 }
133
138 {
139 get { return ZeroElement; }
140 }
141
146 {
147 get { return OneElement; }
148 }
149
156 public override bool TryConvertTo(Type DesiredType, out object Value)
157 {
158 if (DesiredType == typeof(byte))
159 {
160 if (this.value >= byte.MinValue && this.value <= byte.MaxValue)
161 {
162 Value = (byte)this.value;
163 return true;
164 }
165 }
166 else if (DesiredType == typeof(decimal))
167 {
168 Value = (decimal)this.value;
169 return true;
170 }
171 else if (DesiredType == typeof(double))
172 {
173 Value = (double)this.value;
174 return true;
175 }
176 else if (DesiredType == typeof(short))
177 {
178 if (this.value >= short.MinValue && this.value <= short.MaxValue)
179 {
180 Value = (short)this.value;
181 return true;
182 }
183 }
184 else if (DesiredType == typeof(int))
185 {
186 if (this.value >= int.MinValue && this.value <= int.MaxValue)
187 {
188 Value = (int)this.value;
189 return true;
190 }
191 }
192 else if (DesiredType == typeof(long))
193 {
194 if (this.value >= long.MinValue && this.value <= long.MaxValue)
195 {
196 Value = (long)this.value;
197 return true;
198 }
199 }
200 else if (DesiredType == typeof(sbyte))
201 {
202 if (this.value >= sbyte.MinValue && this.value <= sbyte.MaxValue)
203 {
204 Value = (sbyte)this.value;
205 return true;
206 }
207 }
208 else if (DesiredType == typeof(float))
209 {
210 Value = (float)this.value;
211 return true;
212 }
213 else if (DesiredType == typeof(ushort))
214 {
215 if (this.value >= ushort.MinValue && this.value <= ushort.MaxValue)
216 {
217 Value = (ushort)this.value;
218 return true;
219 }
220 }
221 else if (DesiredType == typeof(uint))
222 {
223 if (this.value >= uint.MinValue && this.value <= uint.MaxValue)
224 {
225 Value = (uint)this.value;
226 return true;
227 }
228 }
229 else if (DesiredType == typeof(ulong))
230 {
231 if (this.value >= ulong.MinValue && this.value <= ulong.MaxValue)
232 {
233 Value = (ulong)this.value;
234 return true;
235 }
236 }
237 else if (DesiredType == typeof(DoubleNumber))
238 {
239 Value = this;
240 return true;
241 }
242 else
243 {
244 TypeInfo TI = DesiredType.GetTypeInfo();
245
246 if (TI.IsEnum)
247 {
248 Value = Enum.ToObject(DesiredType, (int)this.value);
249 return true;
250 }
251 else if (TI.IsAssignableFrom(typeof(double).GetTypeInfo()))
252 {
253 Value = this.value;
254 return true;
255 }
256 }
257
258 return Expression.TryConvert(this.value, DesiredType, out Value);
259 }
260 }
261}
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
Base class for all types of field elements.
Definition: FieldElement.cs:10
Class managing a script expression.
Definition: Expression.cs:39
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5268
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
Definition: DoubleNumber.cs:99
override IRingElement Invert()
Inverts the element, if possible.
Definition: DoubleNumber.cs:89
static readonly DoubleNumber ZeroElement
0
Definition: DoubleNumber.cs:16
override IGroupElement Negate()
Negates the element.
override object AssociatedObjectValue
Associated object value.
Definition: DoubleNumber.cs:70
override IAbelianGroupElement Zero
Returns the zero element of the group.
override ICommutativeRingElement Multiply(ICommutativeRingElement Element)
Tries to multiply an element to the current element.
Definition: DoubleNumber.cs:77
override bool Equals(object obj)
Compares the element to another. If elements are equal.
override int GetHashCode()
Calculates a hash code of the element. Hash code.
override ICommutativeRingWithIdentityElement One
Returns the identity element of the commutative ring with identity.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
override IField AssociatedField
Associated Field.
Definition: DoubleNumber.cs:63
static readonly DoubleNumber OneElement
1
Definition: DoubleNumber.cs:21
static readonly DoubleNumber ThreeElement
3
Definition: DoubleNumber.cs:31
DoubleNumber(double Value)
Double-valued number.
Definition: DoubleNumber.cs:39
static readonly DoubleNumber TwoElement
2
Definition: DoubleNumber.cs:26
Pseudo-field of double numbers, as an approximation of the field of real numbers.
static readonly DoubleNumbers Instance
Instance of the set of complex numbers.
Basic interface for all types of abelian group elements.
Basic interface for all types of commutative ring elements.
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for all types of group elements.
Definition: IGroupElement.cs:9
Basic interface for all types of ring elements.
Definition: IRingElement.cs:10
Basic interface for all types of fields.
Definition: IField.cs:9