3

I'm about to start some numerical analysis work in C#, and was considering using operator overloading and classes such as Distance, Speed, Acceleration, and so forth, together with suitably-defined operator overloads, to make sure all natural operations would be permitted and all mistaken ones forbidden, so that

speed = distance / time

would behave as one would expect but

acceleration = distance / time

would not compile (or at least throw an exception).

I can't be the first person to want this so I was wondering if anyone knew of a suitable library available for use?

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
  • It's F# (hence not an answer) but you might be interested in F#'s units of measure. http://msdn.microsoft.com/en-us/library/dd233243.aspx and http://blogs.msdn.com/b/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx. It solves your problem but you'd have to use F#. Also look at http://stackoverflow.com/questions/348853/units-of-measure-in-c-almost – Ray Jul 06 '11 at 09:44
  • I think you'd be better of implementing methods for this, ie. `GetSpeed(double distance, double time) { return distance / time; }`. What you're suggesting here is only going to make things more complex than they need to be. – Jouke van der Maas Jul 06 '11 at 09:46
  • all these examples ignores that speed is actually a vector... – Felice Pollano Jul 06 '11 at 09:50
  • @Felice. No it's not. You're thinking of velocity. – Ray Jul 06 '11 at 09:52
  • @Ray, you are correct, this is a mistake due to my bad english – Felice Pollano Jul 06 '11 at 09:58
  • @Ray, yes, the units-of-measure question is the sort of thing I had in mind, although I wouldn't want units to be nailed down that way; I was thinking more that one would have `speed.MetresPerSecond` or `speed.MilesPerHour` methods or properties if one wanted a unit particularly. – Brian Hooper Jul 06 '11 at 10:04

1 Answers1

0

I think you can achieve this goal by defining new types , let's say

public class MathEntity { /*Operators overlaoding */}
public class Distance : MathEntity { /*Implementation*/}
public class Time   : MathEntity {/* Implementation*/}

Hope this helps.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • That's what he said in the question. He's asking if there's a library so he doesn't have to do it himself. – Ray Jul 06 '11 at 09:49