Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Beta/Math/nb/MathStat.hpp

Go to the documentation of this file.
00001 // Copyright(c) Fred P.                  fprog26 -at- hotmail.com
00002 // Part of J2K Lib, LGPL licensed.       http://j2k.sourceforge.net
00003 // Look at license.txt  OR  http://www.gnu.org/copyleft/lesser.html
00004 // Version 0.1.03july
00005  
00006 #ifndef __J2K__MATH_STATE_HPP__
00007 #define __J2K__MATH_STATE_HPP__
00008 
00009 /////////////////////////////////////////////////////////////////////////////
00010 // Math reminder: x is Real and not Zero.                                  //
00011 // PosInfinity =  Inf +  Inf,  Inf - -Inf,  x/0, x + Inf, (-x)(-Inf), etc. //
00012 // NegInfinity = -Inf + -Inf, -Inf -  Inf, -x/0, x - Inf, (-x)( Inf), etc. //
00013 // NaN         =  Inf/Inf, 0/Inf, Inf/0, -Inf/Inf, Inf/-Inf, etc.          //
00014 // FPError     =  Floating Point arithmetic error.  Precision flaw.        //
00015 /////////////////////////////////////////////////////////////////////////////
00016 // Compare reminder:                                                       //
00017 //  Nothing is bigger than +Inf, so NaN is not bigger than +Inf            //
00018 //  Nothing is less   than -Inf, so NaN is not less   than -Inf            //
00019 /////////////////////////////////////////////////////////////////////////////
00020 //  NaN is not equal to NaN, since the first NaN could be a Real/Infinite  //
00021 //  number different from the second one.  0/0 = 2   and   0/0 = 300 !     //
00022 //  In fact, if you got NaN you should solve the equations in another way. //
00023 //  Let say, using Hopital's rules  OR  other techniques.                  //
00024 /////////////////////////////////////////////////////////////////////////////
00025 //  It might look stupid, but you have to think about it twice             //
00026 //  and verify it on paper when you look or write these tables...          //
00027 /////////////////////////////////////////////////////////////////////////////
00028 //  NOTICE: Before complaining about BUGS, verify it twice on paper.       //
00029 /////////////////////////////////////////////////////////////////////////////
00030 //  FPError is Real and not infinite. ] -inf, +inf [                       //
00031 /////////////////////////////////////////////////////////////////////////////
00032 // "enum" is converted to int by the compiler; however, it's easier        //
00033 // to check that the values are always known and bounded properly.         //
00034 /////////////////////////////////////////////////////////////////////////////
00035 
00036 #define J2K_MAX_MathState  7
00037 
00038 enum MathState {
00039   Zero        = 0,  // Valid states
00040   Positive    = 1,  // Bigger  than 0
00041   Negative    = 2,  // Smaller than 0
00042 
00043   Valid       = 1,  // Calculate it for Add, Sub, Mult, Div, etc.
00044 
00045   PosInfinity = 3,  // Infinite
00046   NegInfinity = 4,
00047 
00048   NaN         = 5,  // Unknown (Can be anything: e.g. 0/0 )
00049 
00050   FPError     = 6,  // Float-Point error happened (i.e. Recalculate it)
00051 
00052   Inf         = 3,  // Abbreviations (No additional cost for it)
00053   PosInf      = 3,  // Think of it as a compiler verified #define.
00054   NegInf      = 4,
00055   Pos         = 1,
00056   Neg         = 2
00057 };
00058 
00059 // return (left) < (right). 2, if (Valid,Valid), 6 for unknown
00060 const int MathStateLessThan[ J2K_MAX_MathState ][ J2K_MAX_MathState ] = {
00061  {  0,  1,  0,  1,  0,  6,  2 },  //  Zero    < ? 0
00062  {  0,  2,  0,  1,  0,  6,  2 },  //  Pos     < ? ] 0, +inf [
00063  {  1,  1,  2,  1,  0,  6,  2 },  //  Neg     < ? ] -inf, 0 [
00064 
00065  {  0,  0,  0,  0,  0,  0,  0 },  //  PosInf  < ? +inf
00066  {  1,  1,  1,  1,  0,  6,  1 },  //  NegInf  < ? -inf
00067  {  6,  6,  6,  6,  0,  6,  6 },  //  NaN     < ? Any Real incl. Inf & NegInf
00068  {  2,  2,  2,  1,  0,  6,  2 }   //  FPError < ? Real with decimal error
00069 };
00070 
00071 // return (left) > (right). 2, if (Valid,Valid), 6 for unknown
00072 const int MathStateBiggerThan[ J2K_MAX_MathState ][ J2K_MAX_MathState ] = {
00073  {  0,  0,  1,  0,  1,  6,  2 },  //  Zero    > ? 0
00074  {  1,  2,  1,  0,  1,  6,  2 },  //  Pos     > ? ] 0, +inf [
00075  {  0,  0,  2,  0,  1,  6,  2 },  //  Neg     > ? ] -inf, 0 [
00076 
00077  {  1,  1,  1,  0,  1,  6,  1 },  //  PosInf  > ? +inf
00078  {  0,  0,  0,  0,  0,  0,  0 },  //  NegInf  > ? -inf
00079  {  6,  6,  6,  0,  6,  6,  6 },  //  NaN     > ? Any Real incl. Inf & NegInf
00080  {  2,  2,  2,  0,  1,  6,  2 }   //  FPError > ? Real with decimal error
00081 };
00082 
00083 const int MathStateEqualTo[ J2K_MAX_MathState ][ J2K_MAX_MathState ] = {
00084  {  1,  0,  0,  0,  0,  6,  2 },  //  Zero    == ? 0
00085  {  0,  2,  0,  0,  0,  6,  2 },  //  Pos     == ? ] 0, +inf [
00086  {  0,  0,  2,  0,  0,  6,  2 },  //  Neg     == ? ] -inf, 0 [
00087 
00088  {  0,  0,  0,  1,  0,  6,  0 },  //  PosInf  == ? +inf
00089  {  0,  0,  0,  0,  1,  6,  0 },  //  NegInf  == ? -inf
00090  {  6,  6,  6,  6,  6,  6,  6 },  //  NaN     == ? Any Real incl. Inf & -Inf
00091  {  2,  2,  2,  0,  0,  6,  2 }   //  FPError == ? Real with decimal error
00092 };
00093 
00094 // return (left) + (right). 
00095 const MathState MathStateAdd[ J2K_MAX_MathState ][ J2K_MAX_MathState ] = {
00096  {    Zero,     Pos,     Neg, PosInf, NegInf, NaN, FPError }, // Zero    + ?
00097  {     Pos,     Pos,   Valid, PosInf, NegInf, NaN, FPError }, // Pos     + ?
00098  {     Neg,   Valid,     Neg, PosInf, NegInf, NaN, FPError }, // Neg     + ?
00099  {  PosInf,  PosInf,  PosInf, PosInf,    NaN, NaN, PosInf  }, // PosInf  + ?
00100  {  NegInf,  NegInf,  NegInf,    NaN, NegInf, NaN, NegInf  }, // NegInf  + ?
00101  {     NaN,     NaN,     NaN,    NaN,    NaN, NaN, NaN     }, // NaN     + ?
00102  { FPError, FPError, FPError, PosInf, NegInf, NaN, FPError }  // FPError + ?
00103 };
00104 
00105 // return (left) - (right). 
00106 const MathState MathStateSub[ J2K_MAX_MathState ][ J2K_MAX_MathState ] = {
00107  {    Zero,     Neg,     Pos, NegInf, PosInf, NaN, FPError }, // Zero    - ?
00108  {     Pos,   Valid,     Pos, NegInf, PosInf, NaN, FPError }, // Pos     - ?
00109  {     Neg,     Neg,   Valid, NegInf, PosInf, NaN, FPError }, // Neg     - ?
00110  {  PosInf,  PosInf,  PosInf,    NaN, PosInf, NaN, PosInf  }, // PosInf  - ?
00111  {  NegInf,  NegInf,  NegInf, NegInf,    NaN, NaN, NegInf  }, // NegInf  - ?
00112  {     NaN,     NaN,     NaN,    NaN,    NaN, NaN, NaN     }, // NaN     - ?
00113  { FPError, FPError, FPError, NegInf, PosInf, NaN, FPError }  // FPError - ?
00114 };
00115 
00116 // return (left) * (right). 
00117 const MathState MathStateMult[ J2K_MAX_MathState ][ J2K_MAX_MathState ] = {
00118  {  Zero,    Zero,    Zero,    NaN,    NaN, NaN, Zero    }, // Zero    * ?
00119  {  Zero,     Pos,     Neg, PosInf, NegInf, NaN, FPError }, // Pos     * ?
00120  {  Zero,     Neg,     Pos, NegInf, PosInf, NaN, FPError }, // Neg     * ?
00121  {   NaN,  PosInf,  NegInf, PosInf, NegInf, NaN, NaN     }, // PosInf  * ?
00122  {   NaN,  NegInf,  PosInf, NegInf, PosInf, NaN, NaN     }, // NegInf  * ?
00123  {   NaN,     NaN,     NaN,    NaN,    NaN, NaN, NaN     }, // NaN     * ?
00124  {  Zero, FPError, FPError,    NaN,    NaN, NaN, FPError }  // FPError * ?
00125 };
00126 
00127 // Inf * FPError     is NaN or Inf => NaN, since FPError could be Zero !
00128 
00129 // return (left) / (right).
00130 const MathState MathStateDiv[ J2K_MAX_MathState ][ J2K_MAX_MathState ] = {
00131  {    NaN,    Zero,    Zero,    NaN,    NaN, NaN, Zero    }, // Zero    / ?
00132  { PosInf,     Pos,     Neg,   Zero,   Zero, NaN, FPError }, // Pos     / ?
00133  { NegInf,     Neg,     Pos,   Zero,   Zero, NaN, FPError }, // Neg     / ?
00134  {    NaN,  PosInf,  NegInf,    NaN,    NaN, NaN, NaN     }, // PosInf  / ?
00135  {    NaN,  NegInf,  PosInf,    NaN,    NaN, NaN, NaN     }, // NegInf  / ?
00136  {    NaN,     NaN,     NaN,    NaN,    NaN, NaN, NaN     }, // NaN     / ?
00137  {    NaN, FPError, FPError,    NaN,    NaN, NaN, NaN     }  // FPError / ?
00138 };
00139 
00140 // Inf / FPError     is NaN, since FPError could be Zero !
00141 // FPError / 0       is NaN, +Inf or -Inf !?
00142 // FPError / FPError is NaN, x/0, 0/x, 0/0, x/x ?!?
00143 
00144 const char* MathStateTxt[ J2K_MAX_MathState ] = {
00145   "Zero",
00146   "Pos",
00147   "Neg",
00148   "Inf",
00149   "-Inf",
00150   "NaN",
00151   "Error"
00152 };
00153 
00154 #endif

Generated on Sun Oct 14 18:46:13 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001