-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replaced the State property with the _normalizationNotApplied field #53
Conversation
- added DataContract/DataMember annotations (with tests)
Numerator = new BigInteger(numerator); | ||
_denominator = BigInteger.One; | ||
State = FractionState.IsNormalized; | ||
_numerator = new BigInteger(numerator); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a normalized fraction using a signed 64bit integer. | ||
/// </summary> | ||
/// <param name="numerator">integer value that will be used for the numerator. The denominator will be 1.</param> | ||
public Fraction(long numerator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the denominator is not explicitly set to BigInteger.One
?
Numerator = new BigInteger(numerator); | ||
_denominator = BigInteger.One; | ||
State = FractionState.IsNormalized; | ||
_numerator = new BigInteger(numerator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the denominator is not explicitly set to BigInteger.One
?
Numerator = new BigInteger(numerator); | ||
_denominator = BigInteger.One; | ||
State = FractionState.IsNormalized; | ||
_numerator = new BigInteger(numerator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the denominator is not explicitly set to BigInteger.One
?
public static Fraction Abs(Fraction fraction) => | ||
new(BigInteger.Abs(fraction.Numerator), BigInteger.Abs(fraction.Denominator), fraction.State); | ||
public static Fraction Abs(Fraction fraction) { | ||
return fraction._normalizationNotApplied |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we would need this additional test. What speaks against the following?
public static Fraction Abs(Fraction fraction) =>
new(fraction._normalizationNotApplied, BigInteger.Abs(fraction.Numerator), BigInteger.Abs(fraction.Denominator));
@@ -11,51 +12,59 @@ namespace Fractions; | |||
/// The data type is not capable to store NaN (not a number) or infinite. | |||
/// </summary> | |||
[TypeConverter(typeof(FractionTypeConverter))] | |||
[StructLayout(LayoutKind.Sequential)] | |||
// [StructLayout(LayoutKind.Sequential)] // TODO do we need a particular order? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is important if the data type is to be transferred via marshaling (P/Invoke). However, there is no (Windows) API that could handle this data type.
Numerator = new BigInteger(numerator); | ||
_denominator = BigInteger.One; | ||
State = FractionState.IsNormalized; | ||
_numerator = new BigInteger(numerator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the denominator is not explicitly set to BigInteger.One
?
Numerator = new BigInteger(numerator); | ||
_denominator = BigInteger.One; | ||
State = FractionState.IsNormalized; | ||
_numerator = new BigInteger(numerator); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a normalized fraction using a big integer. | ||
/// </summary> | ||
/// <param name="numerator">big integer value that will be used for the numerator. The denominator will be 1.</param> | ||
public Fraction(BigInteger numerator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the denominator is not explicitly set to BigInteger.One
?
There is only one usages left of the
Overall, it doesn't look pretty, having the reason with the negated field name... Technically, it doesn't really matter for the |
Changing the default value of I was think of extending the csharp
and /// <summary>
/// The fraction's state.
/// </summary>
public FractionState State =>
(_normalizationNotApplied ? 0x0 : FractionState.IsNormalized) |
(IsInfinity ? FractionState.IsInfinity : 0x0) |
(IsPositiveInfinity ? FractionState.IsPositiveInfinity : 0x0) |
(IsNegativeInfinity ? FractionState.IsNegativeInfinity : 0x0); |
I'm not sure what you mean- if we're talking about serializing the enum then I believe the answer is "it depends on the serializer". If stored as integers, then the extended enum would likely be reconstructed correctly (if extending). If you mean that the code that was previously |
Yes, that is a big one. I mean this: https://learn.microsoft.com/en-us/dotnet/core/compatibility/library-change-rules ❌ DISALLOWED: Changing the value of a public constant or enumeration member |
Extending the |
These are just ideas. To make the |
I have partially adopted the pull request. I removed the DataContract attribute because I don't like the results (both JSON and XML). Strictly speaking, I didn't like the result of serializing BigInteger. |
I don't like the JSON version either, and I don't believe anybody should be using it over the json converter. However the xml-serialization is required for the WCF proxy generation- it's an out of the box thing, and there is no longer a way (since .net core) to create a custom converter (what used to be called a Surrogate). |
I haven't used WCF in years. However, I remember that the XML/JSON serialization could be influenced somehow. Maybe one can do something for the .NET Framework using the An acceptable result (for me) would be something like this: {
"Numerator": "-123456789",
"Denominator": "1",
"NormalizationNotApplied": false
} <Fraction ...>
<Numerator>-123456789</Numerator>
<Denominator>1</Denominator>
<NormalizationNotApplied>false</NormalizationNotApplied>
</Fraction> If a field is |
There isn't any practical way of influencing the way the default Same restrictions apply to the xml-based serialization- WCF requires the type to be a "known type" or a Here is how customizing the data-contracts used to be done, but those types are only available for .NET Framework. |
Oh man, I really hate serialization... Look at the result of the The only way to get a "correct" JSON output, is to use the (obsolete) using System;
using System.Globalization;
using System.Numerics;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace Fractions;
[Serializable]
public partial struct Fraction : ISerializable {
private Fraction(SerializationInfo info, StreamingContext context) {
if (info == null) {
throw new ArgumentNullException(nameof(info));
}
var numerator = BigInteger.Parse(info.GetString(nameof(Numerator)) ?? "0", CultureInfo.InvariantCulture);
var denominator = BigInteger.Parse(info.GetString(nameof(Denominator)) ?? "0", CultureInfo.InvariantCulture);
var normalizationNotApplied = info.GetBoolean("NormalizationNotApplied");
this = new Fraction(normalizationNotApplied, numerator, denominator);
}
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
if (info == null) {
throw new ArgumentNullException(nameof(info));
}
info.AddValue(nameof(Numerator), Numerator.ToString(CultureInfo.InvariantCulture));
info.AddValue(nameof(Denominator), Denominator.ToString(CultureInfo.InvariantCulture));
info.AddValue("NormalizationNotApplied", _normalizationNotApplied);
}
} |
.. as discussed in #51
..and added DataContract/DataMember annotations (with tests)