You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current valueRatio is in form of just pair - numerator (signed) and denominator (unsigned). This was picked to avoid problem present in std :: ratio. That is in std :: ratio each value may be stored in two different ways (despite issue with standard saying that std :: ratio may or may not implement rounding to avoid integers overflow and hence compilation failure, but that is another story). For example:
std :: ratio<-1, -1> vs. std :: ratio<1, 1>,
std :: ratio<1, -1> vs. std :: ratio<-1, 1>.
Although current version of valueRatio does have such problem, it has one problem making implementation quite complex. It operates on two different types (having two different ranges of values). Largest problem appeared on contraction. Greatest common divider algorithms should work on unsigned values, therefore current implementation was:
store sign of numerator,
get absolute value of numerator,
compute gcd of numerator absolute value and denominator,
divide both numerator absolute value and denominator by their gcd,
restore sign of new numerator.
However there is small catch in above algorithm. Sign integers usually have asymmetric range, so absolute value is not always possible. So several concepts appeared:
rewrite algorithm to handle signed numerator (sounds easy, but later gcd must be applied to both numerator and denominator, that are of different types, and this appeared to be quite complex in implementation),
implement rounding (rounding in case of non-numeric operation like contraction sounds like really bad idea),
always work on unsigned values of same type.
After discussing with friends last option won (although i also tried first one, but failed miserably).
Storing sign separately leads to problem of double zero (plus zero and minus zero). However some consider it more feature than bug.
Therefore if you have any idea, comment or anything please join discussion.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Current valueRatio is in form of just pair - numerator (signed) and denominator (unsigned). This was picked to avoid problem present in std :: ratio. That is in std :: ratio each value may be stored in two different ways (despite issue with standard saying that std :: ratio may or may not implement rounding to avoid integers overflow and hence compilation failure, but that is another story). For example:
Although current version of valueRatio does have such problem, it has one problem making implementation quite complex. It operates on two different types (having two different ranges of values). Largest problem appeared on contraction. Greatest common divider algorithms should work on unsigned values, therefore current implementation was:
However there is small catch in above algorithm. Sign integers usually have asymmetric range, so absolute value is not always possible. So several concepts appeared:
After discussing with friends last option won (although i also tried first one, but failed miserably).
Storing sign separately leads to problem of double zero (plus zero and minus zero). However some consider it more feature than bug.
Therefore if you have any idea, comment or anything please join discussion.
Beta Was this translation helpful? Give feedback.
All reactions