Replies: 23 comments
-
I'd prefer the suffix BI for BigInteger and BR for BigRational. |
Beta Was this translation helpful? Give feedback.
-
Does a different opinion on which suffix to use, really justify a downvote? |
Beta Was this translation helpful? Give feedback.
-
I don't see the value of this. Making types "feel" like primitive types is not a worthy goal unless there's some productivity or performance benefit. Even DateTime which is used three or four orders of magnitude more is not treated as a primitive, so why BigInteger? |
Beta Was this translation helpful? Give feedback.
-
Because it is an integer type and so it feels normal to create a new one doing: BigInteger big1 = 12345678901234567890123456789I; than doing: BigInteger big1 = BigInterger.Parse("12345678901234567890123456789"); the fact of using 'I' is simply because there is a precedent in F#, I'm unsure 'BI' could be used being of two characters as literal. |
Beta Was this translation helpful? Give feedback.
-
That's my point though. I really don't think "feel" is an important enough basis to justify the complexity and work of this language feature.
We have |
Beta Was this translation helpful? Give feedback.
-
The compiler wouldn't have to resort to However, I think there should be a substantially high bar for promoting any type to a "primitive". It involves officially enshrining the type within the C# specification. The fact that |
Beta Was this translation helpful? Give feedback.
-
I almost referenced |
Beta Was this translation helpful? Give feedback.
-
Doing: BigInteger big1 = 12345678901234567890123456789I; the parsing will happen at compile time not at runtime! It would be the same thing that declaring an Int32 in this way: int anInt = 42; does parsing to convert "42" in 42... so why we do not always do: int anInt = int.Parse("42"); why then in F# they did this without problems and in C# could not be done?
The first point I think would be solved giving the possibility for a type to define its literals so there is no need to extended the language to do this, the second point effectively is a big problem... from the user of the language point of view the fact that I cannot do this does not make a lot of sense (I was a little deluded that I could use "Color.Black" as a default value... it is a "number" for me that too so why not?). |
Beta Was this translation helpful? Give feedback.
-
Thanks for the clarification. Whilst I see benefit to the literal proposal, I'm with you and @HaloFour in not really seeing any benefit to a |
Beta Was this translation helpful? Give feedback.
-
Not quite. Integers are a CLR primitive, loading them involves a completely different set of IL opcodes. Yes, the compiler parses the text that is "42" and from that emits For This is effectively how the C# compiler handles decimal d = 123456789012345678901234.567890m;
// actually
decimal d = new System.Decimal(1849262357, 1186908593, 669260594, false, 5);
I believe that F# has a lot more leeway regarding language design decisions. While the F# language spec is public it's not expected to be ECMA/ISO standardized as is the case with C# (despite being a few versions behind in that respect). As a functional language F# also targets a different crowd where large math is a more common situation. To note, I'm not opposed to the idea, I'm just laying out what I think are issues that need to be considered. |
Beta Was this translation helpful? Give feedback.
-
I looked into this ages ago. The primary reason i found that this was not worthwhile was that:
As such, there was extremely little gain to doing this since users could just write BigInteger.Parse. IMO, we should make things part of the language when we can at least reasonably expect many to use it, or if it would provide substantial value to the people currently using it*. I'm skeptical with BigInt that that value exists. --
|
Beta Was this translation helpful? Give feedback.
-
DateTime could also be given opportunity to be declared in some way but we would have to supply format option, perhaps something like: |
Beta Was this translation helpful? Give feedback.
-
Also it would tie the project to a specific version of the framework, .net 4.0. With little, if any way to make it compatible with earlier version. |
Beta Was this translation helpful? Give feedback.
-
@elcergey I'm doubtful that anything is more readable than |
Beta Was this translation helpful? Give feedback.
-
I'd prefer more generalized features:
BigInteger big1 = 12345678901234567890123456789I; ↓ a source generator const byte[] big1Value =
{
0x15, 0x81, 0x39, 0x6E, 0xB1, 0xC9, 0xBE, 0x46, 0x32, 0x1B, 0xE4, 0x27
}; // embedded in the User String matadata table
var big1 = new BigInteger(big1Value); |
Beta Was this translation helpful? Give feedback.
-
@AdamSpeight2008 As if someone is using anything below .NET 4.0 nowadays. |
Beta Was this translation helpful? Give feedback.
-
@elcergey Feature tend to target a minimum language version. not framework version. |
Beta Was this translation helpful? Give feedback.
-
@ufcpp surely this could be part of the more generic discourse of "custom literals" in the sense that will make possible to define literals without the need to change the C# language, BigInteger could have the literal defined in this way. |
Beta Was this translation helpful? Give feedback.
-
That's not always true though. For example, value tuples cannot be used with frameworks prior to 4.0 (or maybe 4.5; read conflicting details on this and haven't bothered to test it myself). |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Same goes for lamdas, expressions and so on. Need to test tuples though. |
Beta Was this translation helpful? Give feedback.
-
I would also love to have BigFloat (a struct of bigint and big mantissa). And also Rational being suffixable |
Beta Was this translation helpful? Give feedback.
-
While we're on the subject, a |
Beta Was this translation helpful? Give feedback.
-
@lloydjatkinson The problem with that is it causes boxing on value-types. Which is why I assume they didn't have an public interface Eq<A>
{
bool Equals(A x, A y);
}
public interface Ord<A> : Eq<A>
{
int Compare(A x, A y);
}
public interface Semigroup<A>
{
A Append(A x, A y);
}
public interface Monoid<A> : Semigroup<A>
{
A Empty();
}
public interface Num<A> : Ord<A>, Monoid<A>
{
A Plus(A x, A y);
A Subtract(A x, A y);
A Product(A x, A y);
A Divide(A x, A y);
A Negate(A x);
A FromInteger(int x);
}
public struct TInt : Num<int>
{
public bool Equals(int x, int y) => x == y;
public int Compare(int x, int y) => x.CompareTo(y);
public int Plus(int x, int y) => x + y;
public int Subtract(int x, int y) => x - y;
public int Product(int x, int y) => x * y;
public int Divide(int x, int y) => x / y;
public int Negate(int x) => -x;
public int FromInteger(int x) => x;
public int Empty() => 0;
public int Append(int x, int y) => Plus(x, y);
}
public struct TDouble : Num<double>
{
public bool Equals(double x, double y) => x == y;
public int Compare(double x, double y) => x.CompareTo(y);
public double Plus(double x, double y) => x + y;
public double Subtract(double x, double y) => x - y;
public double Product(double x, double y) => x * y;
public double Divide(double x, double y) => x / y;
public double Negate(double x) => -x;
public double FromInteger(int x) => (double)x;
public double Empty() => 0.0;
public double Append(double x, double y) => Plus(x, y);
} Then you can write generic functions that take static A Double<NumA, A>(A x) where NumA : struct, Num<A> =>
default(NumA).Plus(x, x); When you call it you specify the instance ( int x = 100;
double y = 50.0;
var r1 = Double<TInt, int>(x);
var r2 = Double<TDouble, double>(x); No boxing, no additional memory allocations ( A more complete implementation can be found here with the instances ( |
Beta Was this translation helpful? Give feedback.
-
I propose to add 'I' (as in F#) as a literal biginteger type giving in this way the possibility to declare it as any numerical type (without the need to use TryParse() or obtaining the bignum via computation).
For example:
As in F# to make it feel as a normal int type, the "alias" bigint should exist.
Beta Was this translation helpful? Give feedback.
All reactions