Skip to content

Instantly share code, notes, and snippets.

@mredig mredig/space age.m
Created Oct 9, 2019

Embed
What would you like to do?
//
// main.m
// Space Age
//
// Created by Michael Redig on 10/9/19.
// Copyright © 2019 Red_Egg Productions. All rights reserved.
//
#import <Foundation/Foundation.h>
double mercuryAge(double seconds) {
return (seconds / 31557600) / 0.2408467;
}
double venusAge(double seconds) {
return (seconds / 31557600) / 0.61519726;
}
double earthAge(double seconds) {
return seconds / 31557600;
}
double marsAge(double seconds) {
return (seconds / 31557600) / 1.8808158;
}
double jupiterAge(double seconds) {
return (seconds / 31557600) / 11.862615;
}
double saturnAge(double seconds) {
return (seconds / 31557600) / 29.447498;
}
double uranusAge(double seconds) {
return (seconds / 31557600) / 84.016846;
}
double neptuneAge(double seconds) {
return (seconds / 31557600) / 164.79132;
}
NSString* spaceAger(double seconds) {
NSString* mercury = [NSString stringWithFormat:@"%.02f Mercury years", mercuryAge(seconds)];
NSString* venus = [NSString stringWithFormat:@"%.02f Venus years", venusAge(seconds)];
NSString* earth = [NSString stringWithFormat:@"%.02f Earth years", earthAge(seconds)];
NSString* mars = [NSString stringWithFormat:@"%.02f Mars years", marsAge(seconds)];
NSString* jupiter = [NSString stringWithFormat:@"%.03f Jupiter years", jupiterAge(seconds)];
NSString* saturn = [NSString stringWithFormat:@"%.04f Saturn years", saturnAge(seconds)];
NSString* uranus = [NSString stringWithFormat:@"%.04f Uranus years", uranusAge(seconds)];
NSString* neptune = [NSString stringWithFormat:@"%.04f Neptune years", neptuneAge(seconds)];
return [NSString stringWithFormat:@"With %.0f seconds, you would be %@, %@, %@, %@, %@, %@, %@, and %@ old.", seconds, mercury, venus, earth, mars, jupiter, saturn, uranus, neptune];
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString* spaceAge = spaceAger(31557600 * (31 + (9.0 / 365.0)));
NSLog(@"%@", spaceAge);
}
return 0;
}
@mredig

This comment has been minimized.

Copy link
Owner Author

commented Oct 9, 2019

@PaulSolt

This comment has been minimized.

Copy link

commented Oct 10, 2019

For clarity I would use a variable for the 31557600 value that's used numerous times. That's a magic number and prone to error if it's a literal number (not a constant).

@PaulSolt

This comment has been minimized.

Copy link

commented Oct 10, 2019

Always write NSString *mercury

Use more constants for 31, 9, 365

@mredig

This comment has been minimized.

Copy link
Owner Author

commented Oct 10, 2019

using a constants is a great idea.

not sure what you mean with mercury?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.