PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vigra-1.8.0/doc/vigra/NumericTraits.html

#
HTML | 182 lines | 172 code | 8 blank | 2 comment | 0 complexity | 470e5c7acd332e1eb434889a27f253ff MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html><head><TITLE>vigra - vigra: template&lt;&gt; struct NumericTraits&lt;ArithmeticType&gt;</TITLE>
  3. <link rel=stylesheet type="text/css" href="vigra.css">
  4. <link rel="shortcut icon" href="vigra-icon.ico" />
  5. </head>
  6. <body bgcolor="#f8f0e0" link="#0040b0" vlink="#a00040">
  7. <basefont face="Helvetica,Arial,sans-serif" size=3>
  8. <p align=right>
  9. [ <a href="http://hci.iwr.uni-heidelberg.de/vigra/">VIGRA Homepage</a> |
  10. <a href="functionindex.html">Function Index</a> |
  11. <a href="classes.html">Class Index</a> |
  12. <a href="namespaces.html">Namespaces</a> |
  13. <a href="files.html">File List</a> |
  14. <a href="index.html">Main Page</a> ]
  15. </p>
  16. <!-- Generated by Doxygen 1.6.3 -->
  17. <div class="contents">
  18. <h1><a class="anchor" id="NumericTraits">template&lt;&gt; struct NumericTraits&lt;ArithmeticType&gt; </a></h1><p>Unary traits for promotion, conversion, creation of arithmetic objects.</p>
  19. <p><b>#include</b> &lt;<a class="el" href="numerictraits_8hxx_source.html">vigra/numerictraits.hxx</a>&gt;</p>
  20. <p>This traits class is used derive important properties of an arithmetic type. Consider the following algorithm:</p>
  21. <div class="fragment"><pre class="fragment"> <span class="comment">// calculate the sum of a sequence of bytes</span>
  22. <span class="keywordtype">int</span> sumBytes(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * begin, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * end)
  23. {
  24. <span class="keywordtype">int</span> result = 0;
  25. <span class="keywordflow">for</span>(; begin != end; ++begin) result += *begin;
  26. <span class="keywordflow">return</span> result;
  27. }
  28. </pre></div><p>The return type of this function can not be 'unsigned char' because the summation would very likely overflow. Since we know the source type, we can easily choose 'int' as an appropriate return type. Likewise, we would have chosen 'float' if we had to sum a sequence of floats. If we want to make this algorithm generic, we would like to derive the appropriate return type automatically. This can be done with NumericTraits. The code would look like this (we use <a class="el" href="group__DataAccessors.html">Data Accessors</a> to read the data from the sequence):</p>
  29. <div class="fragment"><pre class="fragment"> <span class="comment">// calculate the sum of any sequence</span>
  30. <span class="keyword">template</span> &lt;<span class="keyword">class</span> Iterator, <span class="keyword">class</span> Accessor&gt;
  31. <span class="keyword">typename</span> vigra::NumericTraits&lt;typename Accessor::value_type&gt;::Promote
  32. sumSequence(Iterator begin, Iterator end, Accessor a)
  33. {
  34. <span class="comment">// an abbreviation</span>
  35. <span class="keyword">typedef</span> vigra::NumericTraits&lt;typename Accessor::value_type&gt; SrcTraits;
  36. <span class="comment">// find out result type</span>
  37. <span class="keyword">typedef</span> <span class="keyword">typename</span> SrcTraits::Promote ResultType;
  38. <span class="comment">// init result to zero</span>
  39. ResultType result = vigra::NumericTraits&lt;ResultType&gt;::zero();
  40. <span class="keywordflow">for</span>(; begin != end; ++begin)
  41. {
  42. <span class="comment">// cast current item to ResultType and add</span>
  43. result += SrcTraits::toPromote(a(begin));
  44. }
  45. <span class="keywordflow">return</span> result;
  46. }
  47. </pre></div><p>In this example NumericTraits is not only used to deduce the ReturnType of the operation, but also to initialize it with the constant 'zero'. This is necessary since we do not know in general, which expression must be used to obtain a zero of some arbitrary type - '<code>ResultType result = 0;</code>' would only work if the ResultType had an constructor taking an '<code>int</code>' argument, and we would not even have any guarantee as to what the semantics of this constructor are. In addition, the traits are used to cast the source type into the promote type.</p>
  48. <p>Similarly, an algorithm that needs multiplication would use the return type <code>RealPromote</code> and the functions <code>one()</code> and <code>toRealPromote()</code>. The following members are defined in <b> <code>NumericTraits&lt;ArithmeticType&gt;</code></b>:</p>
  49. <table class="doxtable">
  50. <tr>
  51. <td><b> <code>typedef ... Type;</code></b> </td><td><p class="starttd"></p>
  52. <p>the type itself</p>
  53. <p class="endtd"></p>
  54. </td></tr>
  55. <tr>
  56. <td><b> <code>typedef ... Promote;</code></b> </td><td><p class="starttd"></p>
  57. <p>promote type for addition and subtraction</p>
  58. <p class="endtd"></p>
  59. </td></tr>
  60. <tr>
  61. <td><b> <code>typedef ... RealPromote;</code></b> </td><td><p class="starttd">promote type for multiplication and division with a real number</p>
  62. <p>(only defined if <code>ArithmeticType</code> supports these operations)</p>
  63. <p class="endtd"></p>
  64. </td></tr>
  65. <tr>
  66. <td><b> <code>typedef ... ComplexPromote;</code></b> </td><td><p class="starttd"></p>
  67. <p>promote type for complex arithmetic</p>
  68. <p class="endtd"></p>
  69. </td></tr>
  70. <tr>
  71. <td><b> <code>typedef ... ValueType;</code></b> </td><td><p class="starttd"></p>
  72. <p>for scalar types: the type itself<br/>
  73. otherwise: typename Type::value_type (if defined)</p>
  74. <p class="endtd"></p>
  75. </td></tr>
  76. <tr>
  77. <td><b> <code>static Promote toPromote(ArithmeticType v);</code></b> </td><td><p class="starttd">convert to <code>Promote</code> type</p>
  78. <p class="endtd"></p>
  79. </td></tr>
  80. <tr>
  81. <td><b> <code>static RealPromote toRealPromote(ArithmeticType v);</code></b> </td><td><p class="starttd">convert to <code>RealPromote</code> type</p>
  82. <p>(only defined if <code>ArithmeticType</code> supports multiplication)</p>
  83. <p class="endtd"></p>
  84. </td></tr>
  85. <tr>
  86. <td><b> <code>static ArithmeticType fromPromote(Promote v);</code></b> </td><td><p class="starttd">convert from <code>Promote</code> type</p>
  87. <p>if <code>v</code> is outside the range of <code>ArithmeticType</code> it is clipped;</p>
  88. <p class="endtd"></p>
  89. </td></tr>
  90. <tr>
  91. <td><b> <code>static ArithmeticType fromRealPromote(RealPromote v);</code></b> </td><td><p class="starttd">convert from <code>RealPromote</code> type</p>
  92. <p>(only defined if <code>ArithmeticType</code> supports multiplication)</p>
  93. <p>if <code>ArithmeticType</code> is an integral type, the result is rounded</p>
  94. <p>if <code>v</code> is outside the range of <code>ArithmeticType</code> it is clipped</p>
  95. <p class="endtd"></p>
  96. </td></tr>
  97. <tr>
  98. <td><b> <code>static ArithmeticType zero();</code></b> </td><td><p class="starttd">create neutral element of addition</p>
  99. <p>i.e. <code>(ArithmeticType a = ...,</code> <code> a + NumericTraits&lt;ArithmeticType&gt;::zero() == a)</code> must always yield <code>true</code></p>
  100. <p class="endtd"></p>
  101. </td></tr>
  102. <tr>
  103. <td><b> <code>static ArithmeticType nonZero();</code></b> </td><td><p class="starttd">create a non-zero element (if multiplication is defined, this yields one())</p>
  104. <p>i.e. <code>(ArithmeticType a = ...,</code> <code> a + NumericTraits&lt;ArithmeticType&gt;::nonZero() == a)</code> must always yield <code>false</code></p>
  105. <p class="endtd"></p>
  106. </td></tr>
  107. <tr>
  108. <td><b> <code>static ArithmeticType <a class="el" href="group__TinyVectorOperators.html#gaec67766e3428b623ce2d8c40a2e0fab9" title="element-wise minimum">min()</a>;</code></b> </td><td><p class="starttd">the smallest number representable in this type.<br/>
  109. Only available if isOrdered is VigraTrueType. For integral types, this equals <code>INT_MIN</code> etc., for real valued types it is <code>-FLT_MAX</code> etc. (<b>not</b> <code>FLT_MIN</code> -- this is the smallest positive <code>float</code>)</p>
  110. <p class="endtd"></p>
  111. </td></tr>
  112. <tr>
  113. <td><b> <code>static ArithmeticType <a class="el" href="group__TinyVectorOperators.html#ga229f7791dfea0511293198040e55c5b2" title="element-wise maximum">max()</a>;</code></b> </td><td><p class="starttd">the largest number representable in this type.<br/>
  114. Only available if isOrdered is VigraTrueType. For integral types, this equals <code>INT_MAX</code> etc., for real valued types it is <code>FLT_MAX</code> etc.</p>
  115. <p class="endtd"></p>
  116. </td></tr>
  117. <tr>
  118. <td><b> <code>static ArithmeticType one();</code></b> </td><td><p class="starttd">create neutral element of multiplication</p>
  119. <p>(only defined if <code>ArithmeticType</code> supports multiplication)</p>
  120. <p>i.e. <code>(ArithmeticType a = ...,</code> <code> a * NumericTraits&lt;ArithmeticType&gt;::one() == a)</code> must always yield <code>true</code></p>
  121. <p class="endtd"></p>
  122. </td></tr>
  123. <tr>
  124. <td><b> <code>typedef ... isIntegral;</code></b> </td><td><p class="starttd">VigraTrueType if <code>ArithmeticType</code> is an integral type, VigraFalseType otherwise</p>
  125. <p class="endtd"></p>
  126. </td></tr>
  127. <tr>
  128. <td><b> <code>typedef ... isScalar;</code></b> </td><td><p class="starttd">VigraTrueType if <code>ArithmeticType</code> is a scalar type, VigraFalseType otherwise</p>
  129. <p class="endtd"></p>
  130. </td></tr>
  131. <tr>
  132. <td></td></tr>
  133. <tr>
  134. <td><b> <code>typedef ... isSigned;</code></b> </td><td><p class="starttd">VigraTrueType if <code>ArithmeticType</code> is a signed type, VigraFalseType otherwise</p>
  135. <p class="endtd"></p>
  136. </td></tr>
  137. <tr>
  138. <td></td></tr>
  139. <tr>
  140. <td><b> <code>typedef ... isOrdered;</code></b> </td><td><p class="starttd">VigraTrueType if <code>ArithmeticType</code> supports <a class="el" href="group__FixedPointOperations.html#gaabd4258d516eb8c9e38f9f97f3160868" title="less than">operator&lt;()</a>, VigraFalseType otherwise</p>
  141. <p class="endtd"></p>
  142. </td></tr>
  143. <tr>
  144. <td><b> <code>typedef ... isComplex;</code></b> </td><td><p class="starttd">VigraTrueType if <code>ArithmeticType</code> is a complex number, VigraFalseType otherwise</p>
  145. <p class="endtd"></p>
  146. </td></tr>
  147. <tr>
  148. <td></td></tr>
  149. </table>
  150. <p>NumericTraits for the built-in types are defined in <b>#include</b> &lt;<a class="el" href="numerictraits_8hxx_source.html">vigra/numerictraits.hxx</a>&gt;</p>
  151. <p>Namespace: vigra </p>
  152. </div>
  153. <!-- footer.html -->
  154. <p>
  155. <table border=0 cellspacing=0 width="100%" bgcolor="#e0d0a0" cellpadding=5>
  156. <tr>
  157. <td>
  158. <p>
  159. <i>&copy; <A HREF="http://hci.iwr.uni-heidelberg.de/people/ukoethe/">Ullrich K&ouml;the</A> (<A
  160. HREF="mailto:ullrich.koethe@iwr.uni-heidelberg.de">ullrich.koethe@iwr.uni-heidelberg.de</A>)</i> <br>
  161. <i><A HREF="http://hci.iwr.uni-heidelberg.de/">
  162. Heidelberg Collaboratory for Image Processing</a>,
  163. University of Heidelberg, Germany</i>
  164. <td>
  165. <p align=right>
  166. <I>html generated using <A HREF="http://www.doxygen.org">doxygen</A> and <A HREF="http://www.python.org">Python</A></I>
  167. <br>
  168. <i>
  169. vigra 1.8.0 (20 Sep 2011)
  170. </i>
  171. </tr>
  172. </table>
  173. </BODY>
  174. </HTML>