c# - Default value for Rational Number struct -


i'm working on simple math library educational purposes , i've implemented struct represents rational number. basic code showing core fields of struct is:

public struct rationalnumber {     private readonly long numerator;     private readonly long denominator;     private bool isdefinitelycoprime;     private static rationalnumber 0 = 0;      public rationalnumber(long numerator, long denominator)     {         this.numerator = numerator;         this.denominator = denominator;         this.isdefinitelycoprime = false;     }      ... } 

currently i'm implementing rationalmatrix which, you've guessed, made of rationalnumber typed elements.

a useful matrix i'm creating static builder identity matrix. code follows:

public static rationalmatrix getidentitymatrix(int dimension) {     rationalnumber[,] values = new rationalnumber[dimension, dimension];      (int = 0; < dimension; i++)        values[i, i] = 1;      return new rationalmatrix(values); } 

the problem not work because default value of rationalnumber not 0/1 0/0 special kind of value (indeterminate form).

obviously 1 solution straightforward , change method to:

public static rationalmatrix getidentitymatrix(int dimension) {     rationalnumber[,] values = new rationalnumber[dimension, dimension];      (int = 0; < dimension; i++)        (int j = i+1 ; j < dimension; j++)        {            values[i, i] = 1;            values[i, j] = rationalnumber.zero;            values[j, i] = rationalnumber.zero;        }         return new rationalmatrix(values); } 

but somehow seems waste of effort i'm initializing values of whole array 2 times. kind of think more elegant somehow make default value of rationalnumber equal 0/1. easy if rationalnumber class, can't think of way when it's struct. missing obvious or there no way avoid having 0/0 default value?

i'd point out not concerned @ code performance (if bottleneck i'd far past goals already). i'm curious know if there construct (unknown me) allows impose arbitrary default values in struct.

edit: typos

edit 2: broaden scope of question

ok, seems there no way impose arbitrary default values in struct input i'm getting , own conclusions based on limited c# knowledge.

can give me clue why structs must behave way? reason or implemented way because no 1 thought specify option define default values?

if not have distinguish between indeterminate 0/0 , other 0/n values, can treat 0/n zero. is, zeros equal makes sense (0/2 equals 0/1), , divisions 0 equal, 1/0 == 2/0.

public struct rationalnumber : iequatable<rationalnumber> {     private readonly long numerator;     private readonly long denominator;      public rationalnumber(long numerator, long denominator)     {         this.numerator = numerator;         this.denominator = denominator;     }      public bool iszero      {         { return numerator == 0; }     }      public bool isinvalid      {         { return denominator == 0 && numerator != 0; }     }      public bool equals(rationalnumber r)     {        if (r.iszero && iszero)          return true;        if (r.isinvalid && isinvalid)          return true;        return denominator == r.denominator && numerator == r.numerator;     }      public bool equals(object o)     {        if (!(o rationalnumber))          return false;        return equals((rationalnumber)o);     }      public int gethashcode()     {        if (iszero)           return 0;        if (isinvalid)          return int32.minvalue;        return ((float)numerator/denominator).gethashcode();     } }    

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -