• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Elegant way to eliminate large conditional?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Elegant way to eliminate large conditional?


  • Subject: Re: Elegant way to eliminate large conditional?
  • From: "John C. Randolph" <email@hidden>
  • Date: Fri, 3 Jan 2003 20:26:55 -0800

On Friday, January 3, 2003, at 07:50 PM, Andrew Merenbach wrote:

I'm developing a program that includes a unit converter which, among other things, can convert from any one of five temperature scales to any of the other five temperature scales. The code is hard to maintain, unfortunately, as I use a rather long conditional (with multiple sub-conditionals) to determine the "to" and "from" scales.

Is there some (elegant) way to use a matrix-like setup (a two-dimensional selector or even an array?) that, given two arguments (a range of five numbers, for each of the temperature scales) will execute a different one of 25 equations or selectors?

This is a linear relationship in all cases, isn't it?

I would pick one canonical scale (say, celsius) and then always convert to that canonical scale and back. It will simplify the code a great deal, (only ten conversion factors to worry about) and if you do everything in double-precision floats, the error from the double conversion is negligible. Indeed, it will be far less than any actual thermometer can resolve.

@interface TemperatureConverter : NSObject
{
float temp;
}

- setCelciusValue:(float) aTemp
{
temp = aTemp;
}
- (float) celciusValue
{
return temp;
}

- setKelvinValue:(float) aTemp
{
temp = aTemp - 273.16;
}

- (float) kelvinValue
{
return aTemp + 273.16;
}

- setFarenheitValue:(float) aTemp
{
temp = (aTemp - 32) * 5/9;
}

.. and so on.
-jcr

John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Elegant way to eliminate large conditional? (From: Andrew Merenbach <email@hidden>)

  • Prev by Date: Re: Elegant way to eliminate large conditional?
  • Next by Date: Re: Elegant way to eliminate large conditional?
  • Previous by thread: Re: Elegant way to eliminate large conditional?
  • Next by thread: Re: Elegant way to eliminate large conditional?
  • Index(es):
    • Date
    • Thread