home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-Developer.iso / NextLibrary / Frameworks / Foundation.framework / Versions / B / Headers / NSDecimal.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-25  |  3.8 KB  |  89 lines

  1. /*    NSDecimal.h
  2.     Public library for large precision base-10 arithmetic
  3.     Copyright 1995-1996, NeXT Software, Inc.  All rights reserved.
  4. */
  5.  
  6. #if !defined(STRICT_OPENSTEP)
  7.  
  8. #import <Foundation/NSDictionary.h>
  9. #import <limits.h>
  10.  
  11. /***************    Type definitions        ***********/
  12.  
  13. // Rounding policies :
  14. // Original
  15. //    value 1.2  1.21  1.25  1.35  1.27
  16. // Plain    1.2  1.2   1.3   1.4   1.3
  17. // Down     1.2  1.2   1.2   1.3   1.2
  18. // Up       1.2  1.3   1.3   1.4   1.3
  19. // Bankers  1.2  1.2   1.2   1.4   1.3
  20.  
  21. typedef enum {
  22.     NSRoundPlain,   // Round up on a tie
  23.     NSRoundDown,    // Always down == truncate
  24.     NSRoundUp,      // Always up
  25.     NSRoundBankers  // on a tie round so last digit is even
  26. } NSRoundingMode;
  27.  
  28. typedef enum {
  29.     NSCalculationNoError = 0,
  30.     NSCalculationLossOfPrecision, // Result lost precision
  31.     NSCalculationUnderflow,       // Result became 0
  32.     NSCalculationOverflow,        // Result exceeds possible representation
  33.     NSCalculationDivideByZero
  34. } NSCalculationError;
  35.  
  36. #define NSDecimalMaxSize (8)
  37.     // Give a precision of at least 38 decimal digits, 128 binary positions.
  38.  
  39. #define NSDecimalNoScale SHRT_MAX
  40.  
  41. typedef struct {
  42.     signed   int _exponent:8;
  43.     unsigned int _length:4;     // length == 0 && isNegative -> NaN
  44.     unsigned int _isNegative:1;
  45.     unsigned int _isCompact:1;
  46.     unsigned int _reserved:18;
  47.     unsigned short _mantissa[NSDecimalMaxSize];
  48. } NSDecimal;
  49.  
  50. static inline BOOL NSDecimalIsNotANumber(const NSDecimal *decimal)
  51.   { return ((decimal->_length == 0) && decimal->_isNegative); }
  52.  
  53. /***************    Operations        ***********/
  54.  
  55. FOUNDATION_EXPORT void NSDecimalCopy(NSDecimal *destination, const NSDecimal *source);
  56.  
  57. FOUNDATION_EXPORT void NSDecimalCompact(NSDecimal *number);
  58.  
  59. FOUNDATION_EXPORT NSComparisonResult NSDecimalCompare(const NSDecimal *leftOperand, const NSDecimal *rightOperand);
  60.     // NSDecimalCompare:Compares leftOperand and rightOperand.
  61.  
  62. FOUNDATION_EXPORT void NSDecimalRound(NSDecimal *result, const NSDecimal *number, int scale, NSRoundingMode roundingMode);
  63.     // Rounds num to the given scale using the given mode.
  64.     // result may be a pointer to same space as num.
  65.     // scale indicates number of significant digits after the decimal point
  66.  
  67. FOUNDATION_EXPORT NSCalculationError NSDecimalNormalize(NSDecimal *number1, NSDecimal *number2, NSRoundingMode roundingMode);
  68.  
  69. FOUNDATION_EXPORT NSCalculationError NSDecimalAdd(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);
  70.     // Exact operations. result may be a pointer to same space as leftOperand or rightOperand
  71.  
  72. FOUNDATION_EXPORT NSCalculationError NSDecimalSubtract(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);
  73.     // Exact operations. result may be a pointer to same space as leftOperand or rightOperand
  74.  
  75. FOUNDATION_EXPORT NSCalculationError NSDecimalMultiply(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);
  76.     // Exact operations. result may be a pointer to same space as leftOperand or rightOperand
  77.  
  78. FOUNDATION_EXPORT NSCalculationError NSDecimalDivide(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);
  79.     // Division could be silently inexact;
  80.     // Exact operations. result may be a pointer to same space as leftOperand or rightOperand
  81.     
  82. FOUNDATION_EXPORT NSCalculationError NSDecimalPower(NSDecimal *result, const NSDecimal *number, unsigned power, NSRoundingMode roundingMode);
  83.  
  84. FOUNDATION_EXPORT NSCalculationError NSDecimalMultiplyByPowerOf10(NSDecimal *result, const NSDecimal *number, short power, NSRoundingMode roundingMode);
  85.  
  86. FOUNDATION_EXPORT NSString *NSDecimalString(const NSDecimal *decimal, NSDictionary *locale);
  87.  
  88. #endif /* !STRICT_OPENSTEP */
  89.