1/* Implementation of class NSUnit 2 Copyright (C) 2019 Free Software Foundation, Inc. 3 4 By: Gregory John Casamento <greg.casamento@gmail.com> 5 Date: Mon Sep 30 15:58:21 EDT 2019 6 7 This file is part of the GNUstep Library. 8 9 This library is free software; you can redistribute it and/or 10 modify it under the terms of the GNU Lesser General Public 11 License as published by the Free Software Foundation; either 12 version 2 of the License, or (at your option) any later version. 13 14 This library is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Lesser General Public License for more details. 18 19 You should have received a copy of the GNU Lesser General Public 20 License along with this library; if not, write to the Free 21 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 22 Boston, MA 02110 USA. 23*/ 24 25#import "Foundation/NSArchiver.h" 26#import "Foundation/NSKeyedArchiver.h" 27#import "Foundation/NSUnit.h" 28 29// Private methods... 30@interface NSDimension (Private) 31- (instancetype) initWithSymbol: (NSString *)symbol 32 coefficient: (double)coefficient 33 constant: (double)constant; 34@end 35 36// Abstract conversion... 37@implementation NSUnitConverter 38- (double) baseUnitValueFromValue: (double)value 39{ 40 return 0.0; 41} 42 43- (double) valueFromBaseUnitValue: (double)baseUnitValue 44{ 45 return 0.0; 46} 47@end 48 49// Linear conversion... 50@implementation NSUnitConverterLinear 51- (instancetype) initWithCoefficient: (double)coefficient 52{ 53 return [self initWithCoefficient: coefficient constant: 0.0]; 54} 55 56- (instancetype) initWithCoefficient: (double)coefficient 57 constant: (double)constant 58{ 59 self = [super init]; 60 if (self != nil) 61 { 62 _coefficient = coefficient; 63 _constant = constant; 64 } 65 return self; 66} 67 68- (id) initWithCoder: (NSCoder *)coder 69{ 70 if ([coder allowsKeyedCoding]) 71 { 72 _coefficient = [coder decodeDoubleForKey: @"NS.coefficient"]; 73 _constant = [coder decodeDoubleForKey: @"NS.constant"]; 74 } 75 else 76 { 77 [coder decodeValueOfObjCType: @encode(double) at: &_coefficient]; 78 [coder decodeValueOfObjCType: @encode(double) at: &_constant]; 79 } 80 return self; 81} 82 83- (void) encodeWithCoder: (NSCoder *)coder 84{ 85 if([coder allowsKeyedCoding]) 86 { 87 [coder encodeDouble: _coefficient forKey: @"NS.coefficient"]; 88 [coder encodeDouble: _constant forKey: @"NS.constant"]; 89 } 90 else 91 { 92 [coder encodeValueOfObjCType: @encode(double) at: &_coefficient]; 93 [coder encodeValueOfObjCType: @encode(double) at: &_constant]; 94 } 95} 96 97- (double) coefficient 98{ 99 return _coefficient; 100} 101 102- (double) constant 103{ 104 return _constant; 105} 106 107- (double) baseUnitValueFromValue: (double)value 108{ 109 return (_coefficient * value) + _constant; 110} 111 112- (double) valueFromBaseUnitValue: (double)baseUnitValue 113{ 114 return (baseUnitValue - _constant) / _coefficient; 115} 116@end 117 118// Abstract unit... 119@implementation NSUnit 120- (instancetype) init 121{ 122 return [self initWithSymbol: @""]; 123} 124 125- (instancetype) initWithSymbol: (NSString *)symbol 126{ 127 self = [super init]; 128 if (self != nil) 129 { 130 ASSIGNCOPY(_symbol, symbol); 131 } 132 return self; 133} 134 135- (id) initWithCoder: (NSCoder *)coder 136{ 137 if ([coder allowsKeyedCoding]) 138 { 139 _symbol = [coder decodeObjectForKey: @"NS.symbol"]; 140 } 141 else 142 { 143 _symbol = [coder decodeObject]; 144 } 145 return self; 146} 147 148- (void) encodeWithCoder: (NSCoder *)coder 149{ 150 if ([coder allowsKeyedCoding]) 151 { 152 [coder encodeObject: _symbol forKey: @"NS.symbol"]; 153 } 154 else 155 { 156 [coder encodeObject: _symbol]; 157 } 158} 159 160- (instancetype) copyWithZone: (NSZone *)zone 161{ 162 return [[NSUnit allocWithZone: zone] initWithSymbol: [self symbol]]; 163} 164 165- (NSString *) symbol 166{ 167 return _symbol; 168} 169@end 170 171 172// Dimension using units.... 173@implementation NSDimension 174- (NSUnitConverter *) converter 175{ 176 return _converter; 177} 178 179- (instancetype) initWithSymbol: (NSString *)symbol converter: (NSUnitConverter *) converter 180{ 181 self = [super initWithSymbol: symbol]; 182 if (self != nil) 183 { 184 ASSIGN(_converter, converter); 185 } 186 return self; 187} 188 189- (instancetype) initWithSymbol: (NSString *)symbol 190 coefficient: (double)coefficient 191 constant: (double)constant 192{ 193 NSUnitConverterLinear *converter = [[NSUnitConverterLinear alloc] initWithCoefficient: coefficient 194 constant: constant]; 195 self = [self initWithSymbol: symbol 196 converter: converter]; 197 198 RELEASE(converter); 199 return self; 200} 201 202+ (instancetype) baseUnit 203{ 204 return nil; 205} 206 207- (id) initWithCoder: (NSCoder *)coder 208{ 209 self = [super initWithCoder: coder]; 210 if ([coder allowsKeyedCoding]) 211 { 212 _converter = [coder decodeObjectForKey: @"NS.converter"]; 213 } 214 else 215 { 216 _symbol = [coder decodeObject]; 217 } 218 return self; 219} 220 221- (void) encodeWithCoder: (NSCoder *)coder 222{ 223 [super encodeWithCoder: coder]; 224 if ([coder allowsKeyedCoding]) 225 { 226 [coder encodeObject: _converter forKey: @"NS.converter"]; 227 } 228 else 229 { 230 [coder encodeObject: _symbol]; 231 } 232} 233 234@end 235 236 237// Predefined.... 238@implementation NSUnitAcceleration 239 240+ (instancetype) baseUnit 241{ 242 return [self metersPerSecondSquared]; 243} 244 245// Base unit - metersPerSecondSquared 246+ (NSUnitAcceleration *) metersPerSecondSquared 247{ 248 return AUTORELEASE([[NSUnitAcceleration alloc] initWithSymbol: @"m/s^2" 249 coefficient: 1.0 250 constant: 0.0]); 251} 252 253+ (NSUnitAcceleration *) gravity 254{ 255 return AUTORELEASE([[NSUnitAcceleration alloc] initWithSymbol: @"g" 256 coefficient: 9.81 257 constant: 0]); 258} 259 260@end 261 262@implementation NSUnitAngle 263 264+ (instancetype) baseUnit 265{ 266 return [self degrees]; 267} 268 269// Base unit - degrees 270+ (NSUnitAngle *) degrees 271{ 272 return AUTORELEASE([[NSUnitAngle alloc] initWithSymbol: @"deg" 273 coefficient: 1.0 274 constant: 0.0]); 275} 276 277+ (NSUnitAngle *) arcMinutes 278{ 279 return AUTORELEASE([[NSUnitAngle alloc] initWithSymbol: @"'" 280 coefficient: 0.016667 281 constant: 0.0]); 282} 283 284+ (NSUnitAngle *) arcSeconds 285{ 286 return AUTORELEASE([[NSUnitAngle alloc] initWithSymbol: @"\"" 287 coefficient: 0.00027778 288 constant: 0.0]); 289} 290 291+ (NSUnitAngle *) radians 292{ 293 return AUTORELEASE([[NSUnitAngle alloc] initWithSymbol: @"rad" 294 coefficient: 57.2958 295 constant: 0.0]); 296} 297 298+ (NSUnitAngle *) gradians 299{ 300 return AUTORELEASE([[NSUnitAngle alloc] initWithSymbol: @"grad" 301 coefficient: 0.9 302 constant: 0.0]); 303} 304 305+ (NSUnitAngle *) revolutions 306{ 307 return AUTORELEASE([[NSUnitAngle alloc] initWithSymbol: @"rev" 308 coefficient: 360.0 309 constant: 0.0]); 310} 311 312@end 313 314@implementation NSUnitArea 315 316+ (instancetype) baseUnit 317{ 318 return [self squareMeters]; 319} 320 321// Base unit - squareMeters 322+ (NSUnitArea *) squareMegameters 323{ 324 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"Mm^2" 325 coefficient: 1e12 326 constant: 0.0]); 327} 328 329+ (NSUnitArea *) squareKilometers 330{ 331 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"km^2" 332 coefficient: 1000000.0 333 constant: 0.0]); 334} 335 336+ (NSUnitArea *) squareMeters 337{ 338 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"m^2" 339 coefficient: 1.0 340 constant: 0.0]); 341} 342 343+ (NSUnitArea *) squareCentimeters 344{ 345 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"cm^2" 346 coefficient: 0.0001 347 constant: 0.0]); 348} 349 350+ (NSUnitArea *) squareMillimeters 351{ 352 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"mm^2" 353 coefficient: 0.000001 354 constant: 0.0]); 355} 356 357+ (NSUnitArea *) squareMicrometers 358{ 359 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"um^2" 360 coefficient: 1e-12 361 constant: 0.0]); 362} 363 364+ (NSUnitArea *) squareNanometers 365{ 366 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"nm^2" 367 coefficient: 1e-18 368 constant: 0.0]); 369} 370 371+ (NSUnitArea *) squareInches 372{ 373 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"in^2" 374 coefficient: 0.00064516 375 constant: 0.0]); 376} 377 378+ (NSUnitArea *) squareFeet 379{ 380 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"ft^2" 381 coefficient: 0.092903 382 constant: 0.0]); 383} 384 385+ (NSUnitArea *) squareYards 386{ 387 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"yd^2" 388 coefficient: 0.836127 389 constant: 0.0]); 390} 391 392+ (NSUnitArea *) squareMiles 393{ 394 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"mi^2" 395 coefficient: 2.59e+6 396 constant: 0.0]); 397} 398 399+ (NSUnitArea *) acres 400{ 401 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"acres" 402 coefficient: 4046.86 403 constant: 0.0]); 404} 405 406+ (NSUnitArea *) ares 407{ 408 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"ares" 409 coefficient: 100.0 410 constant: 0.0]); 411} 412 413+ (NSUnitArea *) hectares 414{ 415 return AUTORELEASE([[NSUnitArea alloc] initWithSymbol: @"hectares" 416 coefficient: 10000.0 417 constant: 0.0]); 418} 419 420@end 421 422@implementation NSUnitConcentrationMass 423 424+ (instancetype) baseUnit 425{ 426 return [self gramsPerLiter]; 427} 428 429// Base unit - gramsPerLiter 430+ (NSUnitConcentrationMass *) gramsPerLiter 431{ 432 return AUTORELEASE([[NSUnitConcentrationMass alloc] initWithSymbol: @"g/L" 433 coefficient: 1.0 434 constant: 0.0]); 435} 436 437+ (NSUnitConcentrationMass *) milligramsPerDeciliter 438{ 439 return AUTORELEASE([[NSUnitConcentrationMass alloc] initWithSymbol: @"mg/dL" 440 coefficient: 0.01 441 constant: 0.0]); 442} 443 444+ (NSUnitConcentrationMass *) millimolesPerLiterWithGramsPerMole:(double)gramsPerMole 445{ 446 return AUTORELEASE([[NSUnitConcentrationMass alloc] initWithSymbol: @"mmol/L" 447 coefficient: 18.0 * gramsPerMole 448 constant: 0.0]); 449} 450 451@end 452 453@implementation NSUnitDispersion 454 455+ (instancetype) baseUnit 456{ 457 return [self partsPerMillion]; 458} 459 460// Base unit - partsPerMillion 461+ (NSUnitDispersion *) partsPerMillion 462{ 463 return AUTORELEASE([[NSUnitDispersion alloc] initWithSymbol: @"ppm" 464 coefficient: 1.0 465 constant: 0.0]); 466} 467 468@end 469 470@implementation NSUnitDuration 471 472+ (instancetype) baseUnit 473{ 474 return [self seconds]; 475} 476 477// Base unit - seconds 478+ (NSUnitDuration *) seconds 479{ 480 return AUTORELEASE([[NSUnitDuration alloc] initWithSymbol: @"sec" 481 coefficient: 1.0 482 constant: 0.0]); 483} 484 485+ (NSUnitDuration *) minutes 486{ 487 return AUTORELEASE([[NSUnitDuration alloc] initWithSymbol: @"min" 488 coefficient: 60.0 489 constant: 0.0]); 490} 491 492+ (NSUnitDuration *) hours 493{ 494 return AUTORELEASE([[NSUnitDuration alloc] initWithSymbol: @"hr" 495 coefficient: 3600.0 496 constant: 0.0]); 497} 498 499@end 500 501@implementation NSUnitElectricCharge 502 503+ (instancetype) baseUnit 504{ 505 return [self coulombs]; 506} 507 508// Base unit - coulombs 509+ (NSUnitElectricCharge *) coulombs 510{ 511 return AUTORELEASE([[NSUnitElectricCharge alloc] initWithSymbol: @"C" 512 coefficient: 1.0 513 constant: 0.0]); 514} 515 516+ (NSUnitElectricCharge *) megaampereHours 517{ 518 return AUTORELEASE([[NSUnitElectricCharge alloc] initWithSymbol: @"MAh" 519 coefficient: 3.6e9 520 constant: 0.0]); 521} 522 523+ (NSUnitElectricCharge *) kiloampereHours 524{ 525 return AUTORELEASE([[NSUnitElectricCharge alloc] initWithSymbol: @"kAh" 526 coefficient: 3600000.0 527 constant: 0.0]); 528} 529 530+ (NSUnitElectricCharge *) ampereHours 531{ 532 return AUTORELEASE([[NSUnitElectricCharge alloc] initWithSymbol: @"mAh" 533 coefficient: 3600.0 534 constant: 0.0]); 535} 536 537+ (NSUnitElectricCharge *) milliampereHours 538{ 539 return AUTORELEASE([[NSUnitElectricCharge alloc] initWithSymbol: @"hr" 540 coefficient: 3.6 541 constant: 0.0]); 542} 543 544+ (NSUnitElectricCharge *) microampereHours 545{ 546 return AUTORELEASE([[NSUnitElectricCharge alloc] initWithSymbol: @"uAh" 547 coefficient: 0.0036 548 constant: 0.0]); 549} 550 551@end 552 553@implementation NSUnitElectricCurrent 554 555+ (instancetype) baseUnit 556{ 557 return [self amperes]; 558} 559 560// Base unit - amperes 561+ (NSUnitElectricCurrent *) megaamperes 562{ 563 return AUTORELEASE([[NSUnitElectricCurrent alloc] initWithSymbol: @"MA" 564 coefficient: 1000000.0 565 constant: 0.0]); 566} 567 568+ (NSUnitElectricCurrent *) kiloamperes 569{ 570 return AUTORELEASE([[NSUnitElectricCurrent alloc] initWithSymbol: @"kA" 571 coefficient: 1000.0 572 constant: 0.0]); 573} 574 575+ (NSUnitElectricCurrent *) amperes 576{ 577 return AUTORELEASE([[NSUnitElectricCurrent alloc] initWithSymbol: @"A" 578 coefficient: 1.0 579 constant: 0.0]); 580} 581 582+ (NSUnitElectricCurrent *) milliamperes 583{ 584 return AUTORELEASE([[NSUnitElectricCurrent alloc] initWithSymbol: @"mA" 585 coefficient: 0.001 586 constant: 0.0]); 587} 588 589+ (NSUnitElectricCurrent *) microamperes 590{ 591 return AUTORELEASE([[NSUnitElectricCurrent alloc] initWithSymbol: @"uA" 592 coefficient: 0.000001 593 constant: 0.0]); 594} 595 596@end 597 598@implementation NSUnitElectricPotentialDifference 599 600+ (instancetype) baseUnit 601{ 602 return [self volts]; 603} 604 605// Base unit - volts 606+ (NSUnitElectricPotentialDifference *) megavolts 607{ 608 return AUTORELEASE([[NSUnitElectricPotentialDifference alloc] initWithSymbol: @"MV" 609 coefficient: 1000000.0 610 constant: 0.0]); 611} 612 613+ (NSUnitElectricPotentialDifference *) kilovolts 614{ 615 return AUTORELEASE([[NSUnitElectricPotentialDifference alloc] initWithSymbol: @"kV" 616 coefficient: 1000.0 617 constant: 0.0]); 618} 619 620+ (NSUnitElectricPotentialDifference *) volts 621{ 622 return AUTORELEASE([[NSUnitElectricPotentialDifference alloc] initWithSymbol: @"V" 623 coefficient: 1.0 624 constant: 0.0]); 625} 626 627+ (NSUnitElectricPotentialDifference *) millivolts 628{ 629 return AUTORELEASE([[NSUnitElectricPotentialDifference alloc] initWithSymbol: @"mV" 630 coefficient: 0.001 631 constant: 0.0]); 632} 633 634+ (NSUnitElectricPotentialDifference *) microvolts 635{ 636 return AUTORELEASE([[NSUnitElectricPotentialDifference alloc] initWithSymbol: @"uV" 637 coefficient: 0.000001 638 constant: 0.0]); 639} 640 641@end 642 643@implementation NSUnitElectricResistance 644 645+ (instancetype) baseUnit 646{ 647 return [self ohms]; 648} 649 650// Base unit - ohms 651+ (NSUnitElectricResistance *) megaohms 652{ 653 return AUTORELEASE([[NSUnitElectricResistance alloc] initWithSymbol: @"MOhm" 654 coefficient: 100000.0 655 constant: 0.0]); 656} 657 658+ (NSUnitElectricResistance *) kiloohms 659{ 660 return AUTORELEASE([[NSUnitElectricResistance alloc] initWithSymbol: @"kOhm" 661 coefficient: 1000.0 662 constant: 0.0]); 663} 664 665+ (NSUnitElectricResistance *) ohms 666{ 667 return AUTORELEASE([[NSUnitElectricResistance alloc] initWithSymbol: @"Ohm" 668 coefficient: 1.0 669 constant: 0.0]); 670} 671 672+ (NSUnitElectricResistance *) milliohms 673{ 674 return AUTORELEASE([[NSUnitElectricResistance alloc] initWithSymbol: @"mOhm" 675 coefficient: 0.001 676 constant: 0.0]); 677} 678 679+ (NSUnitElectricResistance *) microohms 680{ 681 return AUTORELEASE([[NSUnitElectricResistance alloc] initWithSymbol: @"uOhm" 682 coefficient: 0.000001 683 constant: 0.0]); 684} 685 686 687@end 688 689@implementation NSUnitEnergy 690 691+ (instancetype) baseUnit 692{ 693 return [self joules]; 694} 695 696// Base unit - joules 697+ (NSUnitEnergy *) kilojoules 698{ 699 return AUTORELEASE([[NSUnitEnergy alloc] initWithSymbol: @"kJ" 700 coefficient: 1000.0 701 constant: 0.0]); 702} 703 704+ (NSUnitEnergy *) joules 705{ 706 return AUTORELEASE([[NSUnitEnergy alloc] initWithSymbol: @"J" 707 coefficient: 1.0 708 constant: 0.0]); 709} 710 711+ (NSUnitEnergy *) kilocalories 712{ 713 return AUTORELEASE([[NSUnitEnergy alloc] initWithSymbol: @"kCal" 714 coefficient: 4184.0 715 constant: 0.0]); 716} 717 718+ (NSUnitEnergy *) calories 719{ 720 return AUTORELEASE([[NSUnitEnergy alloc] initWithSymbol: @"cal" 721 coefficient: 4.184 722 constant: 0.0]); 723} 724 725+ (NSUnitEnergy *) kilowattHours 726{ 727 return AUTORELEASE([[NSUnitEnergy alloc] initWithSymbol: @"kWh" 728 coefficient: 3600000.0 729 constant: 0.0]); 730} 731 732@end 733 734@implementation NSUnitFrequency 735 736+ (instancetype) baseUnit 737{ 738 return [self hertz]; 739} 740 741// Base unit - hertz 742 743+ (NSUnitFrequency *) terahertz 744{ 745 return AUTORELEASE([[NSUnitFrequency alloc] initWithSymbol: @"thz" 746 coefficient: 1e12 747 constant: 0.0]); 748} 749 750+ (NSUnitFrequency *) gigahertz 751{ 752 return AUTORELEASE([[NSUnitFrequency alloc] initWithSymbol: @"ghz" 753 coefficient: 1e9 754 constant: 0.0]); 755} 756 757+ (NSUnitFrequency *) megahertz 758{ 759 return AUTORELEASE([[NSUnitFrequency alloc] initWithSymbol: @"GHz" 760 coefficient: 1000000.0 761 constant: 0.0]); 762} 763 764+ (NSUnitFrequency *) kilohertz 765{ 766 return AUTORELEASE([[NSUnitFrequency alloc] initWithSymbol: @"KHz" 767 coefficient: 1000.0 768 constant: 0.0]); 769} 770 771+ (NSUnitFrequency *) hertz 772{ 773 return AUTORELEASE([[NSUnitFrequency alloc] initWithSymbol: @"Hz" 774 coefficient: 1.0 775 constant: 0.0]); 776} 777 778+ (NSUnitFrequency *) millihertz 779{ 780 NSUnitFrequency *result = [[NSUnitFrequency alloc] initWithSymbol: @"mHz" 781 coefficient: 0.001 782 constant: 0.0]; 783 return result; 784} 785 786+ (NSUnitFrequency *) microhertz 787{ 788 return AUTORELEASE([[NSUnitFrequency alloc] initWithSymbol: @"uHz" 789 coefficient: 0.000001 790 constant: 0.0]); 791} 792 793+ (NSUnitFrequency *) nanohertz 794{ 795 return AUTORELEASE([[NSUnitFrequency alloc] initWithSymbol: @"nHz" 796 coefficient: 1e-9 797 constant: 0.0]); 798} 799 800@end 801 802@implementation NSUnitFuelEfficiency 803 804+ (instancetype) baseUnit 805{ 806 return [self litersPer100Kilometers]; 807} 808 809// Base unit - litersPer100Kilometers 810 811+ (NSUnitFuelEfficiency *) litersPer100Kilometers 812{ 813 return AUTORELEASE([[NSUnitFuelEfficiency alloc] initWithSymbol: @"L/100km" 814 coefficient: 1.0 815 constant: 0.0]); 816} 817 818+ (NSUnitFuelEfficiency *) milesPerImperialGallon 819{ 820 // FIXME 821 return AUTORELEASE([[NSUnitFuelEfficiency alloc] initWithSymbol: @"mpg" 822 coefficient: 0.0 823 constant: 0.0]); 824} 825 826+ (NSUnitFuelEfficiency *) milesPerGallon 827{ 828 // FIXME 829 return AUTORELEASE([[NSUnitFuelEfficiency alloc] initWithSymbol: @"mpg" 830 coefficient: 0.0 831 constant: 0.0]); 832} 833 834@end 835 836@implementation NSUnitLength 837 838+ (instancetype) baseUnit 839{ 840 return [self meters]; 841} 842 843// Base unit - meters 844 845+ (NSUnitLength *) megameters 846{ 847 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"Mm" 848 coefficient: 1000000.0 849 constant: 0.0]); 850} 851 852+ (NSUnitLength *) kilometers 853{ 854 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"kM" 855 coefficient: 1000.0 856 constant: 0.0]); 857} 858 859+ (NSUnitLength *) hectometers 860{ 861 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"hm" 862 coefficient: 100.0 863 constant: 0.0]); 864} 865 866+ (NSUnitLength *) decameters 867{ 868 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"dam" 869 coefficient: 10 870 constant: 0.0]); 871} 872 873+ (NSUnitLength *) meters 874{ 875 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"meters" 876 coefficient: 1.0 877 constant: 0.0]); 878} 879 880+ (NSUnitLength *) decimeters 881{ 882 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"dm" 883 coefficient: 0.1 884 constant: 0.0]); 885} 886 887+ (NSUnitLength *) centimeters 888{ 889 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"cm" 890 coefficient: 0.01 891 constant: 0.0]); 892} 893 894+ (NSUnitLength *) millimeters 895{ 896 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"mm" 897 coefficient: 0.001 898 constant: 0.0]); 899} 900 901+ (NSUnitLength *) micrometers 902{ 903 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"um" 904 coefficient: 0.000001 905 constant: 0.0]); 906} 907 908+ (NSUnitLength *) nanometers 909{ 910 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"nm" 911 coefficient: 1e-9 912 constant: 0.0]); 913} 914 915+ (NSUnitLength *) picometers 916{ 917 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"pm" 918 coefficient: 1e-12 919 constant: 0.0]); 920} 921 922+ (NSUnitLength *) inches 923{ 924 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"in" 925 coefficient: 0.254 926 constant: 0.0]); 927} 928 929+ (NSUnitLength *) feet 930{ 931 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"ft" 932 coefficient: 0.3048 933 constant: 0.0]); 934} 935 936+ (NSUnitLength *) yards 937{ 938 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"yd" 939 coefficient: 0.9144 940 constant: 0.0]); 941} 942 943+ (NSUnitLength *) miles 944{ 945 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"mi" 946 coefficient: 1609.34 947 constant: 0.0]); 948} 949 950+ (NSUnitLength *) scandinavianMiles 951{ 952 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"smi" 953 coefficient: 10000 954 constant: 0.0]); 955} 956 957+ (NSUnitLength *) lightyears 958{ 959 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"ly" 960 coefficient: 9.461e+15 961 constant: 0.0]); 962} 963 964+ (NSUnitLength *) nauticalMiles 965{ 966 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"NM" 967 coefficient: 1852.0 968 constant: 0.0]); 969} 970 971+ (NSUnitLength *) fathoms 972{ 973 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"ftm" 974 coefficient: 1.8288 975 constant: 0.0]); 976} 977 978+ (NSUnitLength *) furlongs 979{ 980 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"fur" 981 coefficient: 201.168 982 constant: 0.0]); 983} 984 985+ (NSUnitLength *) astronomicalUnits 986{ 987 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"ua" 988 coefficient: 1.496e+11 989 constant: 0.0]); 990} 991 992+ (NSUnitLength *) parsecs 993{ 994 return AUTORELEASE([[NSUnitLength alloc] initWithSymbol: @"pc" 995 coefficient: 3.086e+16 996 constant: 0.0]); 997} 998 999@end 1000 1001@implementation NSUnitIlluminance 1002 1003+ (instancetype) baseUnit 1004{ 1005 return [self lux]; 1006} 1007 1008// Base unit - lux 1009+ (NSUnitIlluminance *) lux 1010{ 1011 return AUTORELEASE([[NSUnitIlluminance alloc] initWithSymbol: @"lux" 1012 coefficient: 1.0 1013 constant: 0.0]); 1014} 1015 1016@end 1017 1018@implementation NSUnitMass 1019 1020+ (instancetype) baseUnit 1021{ 1022 return [self kilograms]; 1023} 1024 1025// Base unit - kilograms 1026 1027+ (NSUnitMass *) kilograms 1028{ 1029 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"kg" 1030 coefficient: 1.0 1031 constant: 0.0]); 1032} 1033 1034+ (NSUnitMass *) grams 1035{ 1036 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"g" 1037 coefficient: 0.001 1038 constant: 0.0]); 1039} 1040 1041+ (NSUnitMass *) decigrams 1042{ 1043 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"dg" 1044 coefficient: 0.0001 1045 constant: 0.0]); 1046} 1047 1048+ (NSUnitMass *) centigrams 1049{ 1050 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"cg" 1051 coefficient: 0.00001 1052 constant: 0.0]); 1053} 1054 1055+ (NSUnitMass *) milligrams 1056{ 1057 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"mg" 1058 coefficient: 0.000001 1059 constant: 0.0]); 1060} 1061 1062+ (NSUnitMass *) micrograms 1063{ 1064 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"ug" 1065 coefficient: 1e-9 1066 constant: 0.0]); 1067} 1068 1069+ (NSUnitMass *) nanograms 1070{ 1071 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"ng" 1072 coefficient: 1e-12 1073 constant: 0.0]); 1074} 1075 1076+ (NSUnitMass *) picograms 1077{ 1078 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"pg" 1079 coefficient: 1e-15 1080 constant: 0.0]); 1081} 1082 1083+ (NSUnitMass *) ounces 1084{ 1085 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"oz" 1086 coefficient: 0.0283495 1087 constant: 0.0]); 1088} 1089 1090+ (NSUnitMass *) pounds 1091{ 1092 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"lb" 1093 coefficient: 0.453592 1094 constant: 0.0]); 1095} 1096 1097+ (NSUnitMass *) stones 1098{ 1099 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"st" 1100 coefficient: 6.35029 1101 constant: 0.0]); 1102} 1103 1104+ (NSUnitMass *) metricTons 1105{ 1106 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"t" 1107 coefficient: 1000 1108 constant: 0.0]); 1109} 1110 1111+ (NSUnitMass *) shortTons 1112{ 1113 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"ton" 1114 coefficient: 907.185 1115 constant: 0.0]); 1116} 1117 1118+ (NSUnitMass *) carats 1119{ 1120 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"ct" 1121 coefficient: 0.0002 1122 constant: 0.0]); 1123} 1124 1125+ (NSUnitMass *) ouncesTroy 1126{ 1127 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"oz t" 1128 coefficient: 0.03110348 1129 constant: 0.0]); 1130} 1131 1132+ (NSUnitMass *) slugs 1133{ 1134 return AUTORELEASE([[NSUnitMass alloc] initWithSymbol: @"slug" 1135 coefficient: 14.5939 1136 constant: 0.0]); 1137} 1138 1139@end 1140 1141@implementation NSUnitPower 1142 1143+ (instancetype) baseUnit 1144{ 1145 return [self watts]; 1146} 1147 1148// Base unit - watts 1149 1150+ (NSUnitPower *) terawatts 1151{ 1152 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"TW" 1153 coefficient: 1e12 1154 constant: 0.0]); 1155} 1156 1157+ (NSUnitPower *) gigawatts 1158{ 1159 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"GW" 1160 coefficient: 1e9 1161 constant: 0.0]); 1162} 1163 1164+ (NSUnitPower *) megawatts 1165{ 1166 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"MW" 1167 coefficient: 1000000.0 1168 constant: 0.0]); 1169} 1170 1171+ (NSUnitPower *) kilowatts 1172{ 1173 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"kW" 1174 coefficient: 1000.0 1175 constant: 0.0]); 1176} 1177 1178+ (NSUnitPower *) watts 1179{ 1180 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"W" 1181 coefficient: 1.0 1182 constant: 0.0]); 1183} 1184 1185+ (NSUnitPower *) milliwatts 1186{ 1187 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"mW" 1188 coefficient: 0.001 1189 constant: 0.0]); 1190} 1191 1192+ (NSUnitPower *) microwatts 1193{ 1194 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"uW" 1195 coefficient: 0.000001 1196 constant: 0.0]); 1197} 1198 1199+ (NSUnitPower *) nanowatts 1200{ 1201 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"nW" 1202 coefficient: 1e-9 1203 constant: 0.0]); 1204} 1205 1206+ (NSUnitPower *) picowatts 1207{ 1208 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"pW" 1209 coefficient: 1e-12 1210 constant: 0.0]); 1211} 1212 1213+ (NSUnitPower *) femtowatts 1214{ 1215 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"fW" 1216 coefficient: 1e-15 1217 constant: 0.0]); 1218} 1219 1220+ (NSUnitPower *) horsepower 1221{ 1222 return AUTORELEASE([[NSUnitPower alloc] initWithSymbol: @"hp" 1223 coefficient: 745.7 1224 constant: 0.0]); 1225} 1226 1227@end 1228 1229@implementation NSUnitPressure 1230 1231+ (instancetype) baseUnit 1232{ 1233 return [self newtonsPerMetersSquared]; 1234} 1235 1236// Base unit - newtonsPerMetersSquared (equivalent to 1 pascal) 1237 1238+ (NSUnitPressure *) newtonsPerMetersSquared 1239{ 1240 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"N/m^2" 1241 coefficient: 1.0 1242 constant: 0.0]); 1243} 1244 1245+ (NSUnitPressure *) gigapascals 1246{ 1247 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"GPa" 1248 coefficient: 1e9 1249 constant: 0.0]); 1250} 1251 1252+ (NSUnitPressure *) megapascals 1253{ 1254 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"MPa" 1255 coefficient: 1000000.0 1256 constant: 0.0]); 1257} 1258 1259+ (NSUnitPressure *) kilopascals 1260{ 1261 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"kPa" 1262 coefficient: 1000.0 1263 constant: 0.0]); 1264} 1265 1266+ (NSUnitPressure *) hectopascals 1267{ 1268 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"hPa" 1269 coefficient: 100.0 1270 constant: 0.0]); 1271} 1272 1273+ (NSUnitPressure *) inchesOfMercury 1274{ 1275 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"inHg" 1276 coefficient: 3386.0 1277 constant: 0.0]); 1278} 1279 1280+ (NSUnitPressure *) bars 1281{ 1282 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"bars" 1283 coefficient: 100000.0 1284 constant: 0.0]); 1285} 1286 1287+ (NSUnitPressure *) millibars 1288{ 1289 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"mbars" 1290 coefficient: 100.0 1291 constant: 0.0]); 1292} 1293 1294+ (NSUnitPressure *) millimetersOfMercury 1295{ 1296 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"mmHg" 1297 coefficient: 133.322 1298 constant: 0.0]); 1299} 1300 1301+ (NSUnitPressure *) poundsForcePerSquareInch 1302{ 1303 return AUTORELEASE([[NSUnitPressure alloc] initWithSymbol: @"psi" 1304 coefficient: 6894.76 1305 constant: 0.0]); 1306} 1307 1308 1309@end 1310 1311@implementation NSUnitSpeed 1312+ (instancetype) baseUnit 1313{ 1314 return [self metersPerSecond]; 1315} 1316 1317// Base unit - metersPerSecond 1318+ (NSUnitSpeed *) metersPerSecond 1319{ 1320 return AUTORELEASE([[NSUnitSpeed alloc] initWithSymbol: @"m/s" 1321 coefficient: 1.0 1322 constant: 0.0]); 1323} 1324 1325+ (NSUnitSpeed *) kilometersPerHour 1326{ 1327 return AUTORELEASE([[NSUnitSpeed alloc] initWithSymbol: @"km/h" 1328 coefficient: 0.277778 1329 constant: 0.0]); 1330} 1331 1332+ (NSUnitSpeed *) milesPerHour 1333{ 1334 return AUTORELEASE([[NSUnitSpeed alloc] initWithSymbol: @"mph" 1335 coefficient: 0.44704 1336 constant: 0.0]); 1337} 1338 1339+ (NSUnitSpeed *) knots 1340{ 1341 return AUTORELEASE([[NSUnitSpeed alloc] initWithSymbol: @"kn" 1342 coefficient: 0.51444 1343 constant: 0.0]); 1344} 1345 1346@end 1347 1348@implementation NSUnitTemperature 1349+ (instancetype) baseUnit 1350{ 1351 return [self kelvin]; 1352} 1353 1354// Base unit - kelvin 1355+ (NSUnitTemperature *) kelvin 1356{ 1357 return AUTORELEASE([[NSUnitTemperature alloc] initWithSymbol: @"K" 1358 coefficient: 1.0 1359 constant: 0.0]); 1360} 1361 1362+ (NSUnitTemperature *) celsius 1363{ 1364 return AUTORELEASE([[NSUnitTemperature alloc] initWithSymbol: @"C" 1365 coefficient: 1.0 1366 constant: 273.15]); 1367} 1368 1369+ (NSUnitTemperature *) fahrenheit 1370{ 1371 return AUTORELEASE([[NSUnitTemperature alloc] initWithSymbol: @"F" 1372 coefficient: 0.55555555555556 1373 constant: 255.37222222222427]); 1374} 1375@end 1376 1377@implementation NSUnitVolume 1378+ (instancetype) baseUnit 1379{ 1380 return [self liters]; 1381} 1382 1383// Base unit - liters 1384+ (NSUnitVolume *) megaliters 1385{ 1386 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"ML" 1387 coefficient: 1000000.0 1388 constant: 0.0]); 1389} 1390 1391+ (NSUnitVolume *) kiloliters 1392{ 1393 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"kL" 1394 coefficient: 1000.0 1395 constant: 0.0]); 1396} 1397 1398+ (NSUnitVolume *) liters 1399{ 1400 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"L" 1401 coefficient: 1.0 1402 constant: 0.0]); 1403} 1404 1405+ (NSUnitVolume *) deciliters 1406{ 1407 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"dL" 1408 coefficient: 0.1 1409 constant: 0.0]); 1410} 1411 1412+ (NSUnitVolume *) centiliters 1413{ 1414 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"cL" 1415 coefficient: 0.01 1416 constant: 0.0]); 1417} 1418 1419+ (NSUnitVolume *) milliliters 1420{ 1421 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"mL" 1422 coefficient: 0.001 1423 constant: 0.0]); 1424} 1425 1426+ (NSUnitVolume *) cubicKilometers 1427{ 1428 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"km^3" 1429 coefficient: 1e12 1430 constant: 0.0]); 1431} 1432 1433+ (NSUnitVolume *) cubicMeters 1434{ 1435 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"m^3" 1436 coefficient: 1000.0 1437 constant: 0.0]); 1438} 1439 1440+ (NSUnitVolume *) cubicDecimeters 1441{ 1442 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"dm^3" 1443 coefficient: 1.0 1444 constant: 0.0]); 1445} 1446 1447+ (NSUnitVolume *) cubicCentimeters 1448{ 1449 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"cm^3" 1450 coefficient: 0.0001 1451 constant: 0.0]); 1452} 1453 1454+ (NSUnitVolume *) cubicMillimeters 1455{ 1456 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"mm^3" 1457 coefficient: 0.000001 1458 constant: 0.0]); 1459} 1460 1461+ (NSUnitVolume *) cubicInches 1462{ 1463 NSUnitVolume *result = [[NSUnitVolume alloc] initWithSymbol: @"in^3" 1464 coefficient: 0.0163871 1465 constant: 0.0]; 1466 return result; 1467} 1468 1469+ (NSUnitVolume *) cubicFeet 1470{ 1471 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"ft^3" 1472 coefficient: 28.3168 1473 constant: 0.0]); 1474} 1475 1476+ (NSUnitVolume *) cubicYards 1477{ 1478 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"yd^3" 1479 coefficient: 764.555 1480 constant: 0.0]); 1481} 1482 1483+ (NSUnitVolume *) cubicMiles 1484{ 1485 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"mi^3" 1486 coefficient: 4.168e+12 1487 constant: 0.0]); 1488} 1489 1490+ (NSUnitVolume *) acreFeet 1491{ 1492 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"af" 1493 coefficient: 1.233e+6 1494 constant: 0.0]); 1495} 1496 1497+ (NSUnitVolume *) bushels 1498{ 1499 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"bsh" 1500 coefficient: 32.2391 1501 constant: 0.0]); 1502} 1503 1504+ (NSUnitVolume *) teaspoons 1505{ 1506 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"tsp" 1507 coefficient: 0.00492892 1508 constant: 0.0]); 1509} 1510 1511+ (NSUnitVolume *) tablespoons 1512{ 1513 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"tbsp" 1514 coefficient: 0.0147868 1515 constant: 0.0]); 1516} 1517 1518+ (NSUnitVolume *) fluidOunces 1519{ 1520 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"fl oz" 1521 coefficient: 0.295735 1522 constant: 0.0]); 1523} 1524 1525+ (NSUnitVolume *) cups 1526{ 1527 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"cups" 1528 coefficient: 0.24 1529 constant: 0.0]); 1530} 1531 1532+ (NSUnitVolume *) pints 1533{ 1534 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"pt" 1535 coefficient: 0.473176 1536 constant: 0.0]); 1537} 1538 1539+ (NSUnitVolume *) quarts 1540{ 1541 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"qt" 1542 coefficient: 0.946353 1543 constant: 0.0]); 1544} 1545 1546+ (NSUnitVolume *) gallons 1547{ 1548 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"gal" 1549 coefficient: 3.78541 1550 constant: 0.0]); 1551} 1552 1553+ (NSUnitVolume *) imperialTeaspoons 1554{ 1555 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"tsp" 1556 coefficient: 0.00591939 1557 constant: 0.0]); 1558} 1559 1560+ (NSUnitVolume *) imperialTablespoons 1561{ 1562 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"tbsp" 1563 coefficient: 0.0177582 1564 constant: 0.0]); 1565} 1566 1567+ (NSUnitVolume *) imperialFluidOunces 1568{ 1569 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"fl oz" 1570 coefficient: 0.0284131 1571 constant: 0.0]); 1572} 1573 1574+ (NSUnitVolume *) imperialPints 1575{ 1576 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"pt" 1577 coefficient: 0.568261 1578 constant: 0.0]); 1579} 1580 1581+ (NSUnitVolume *) imperialQuarts 1582{ 1583 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"qt" 1584 coefficient: 1.13652 1585 constant: 0.0]); 1586} 1587 1588+ (NSUnitVolume *) imperialGallons 1589{ 1590 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"gal" 1591 coefficient: 4.54609 1592 constant: 0.0]); 1593} 1594 1595+ (NSUnitVolume *) metricCups 1596{ 1597 return AUTORELEASE([[NSUnitVolume alloc] initWithSymbol: @"metric cup" 1598 coefficient: 0.25 1599 constant: 0.0]); 1600} 1601 1602@end 1603