/usr/share/doc/allegro4-doc/html/alleg032.html is in allegro4-doc 2:4.4.2-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>
Allegro Manual: Fixed point math routines
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" title="Default" type="text/css" href="allegro.css"></head><body bgcolor=white text=black link="#0000ee" alink="#ff0000" vlink="#551a8b">
<h1><a name="Fixed point math routines">Fixed point math routines</a></h1>
<ul>
<li><a href="#fixacos">fixacos</a> — Fixed point inverse cosine lookup table.
<li><a href="#fixadd">fixadd</a> — Safe function to add fixed point numbers clamping overflow.
<li><a href="#fixasin">fixasin</a> — Fixed point inverse sine lookup table.
<li><a href="#fixatan">fixatan</a> — Fixed point inverse tangent lookup table.
<li><a href="#fixatan2">fixatan2</a> — Fixed point version of the libc atan2() routine.
<li><a href="#fixceil">fixceil</a> — Returns the smallest integer not less than x.
<li><a href="#fixcos">fixcos</a> — Fixed point cosine of binary angles.
<li><a href="#fixdiv">fixdiv</a> — Fixed point division.
<li><a href="#fixfloor">fixfloor</a> — Returns the greatest integer not greater than x.
<li><a href="#fixhypot">fixhypot</a> — Fixed point hypotenuse.
<li><a href="#fixmul">fixmul</a> — Multiplies two fixed point values together.
<li><a href="#fixsin">fixsin</a> — Fixed point sine of binary angles.
<li><a href="#fixsqrt">fixsqrt</a> — Fixed point square root.
<li><a href="#fixsub">fixsub</a> — Safe function to subtract fixed point numbers clamping underflow.
<li><a href="#fixtan">fixtan</a> — Fixed point tangent of binary angles.
<li><a href="#fixtof">fixtof</a> — Converts a fixed point to floating point.
<li><a href="#fixtoi">fixtoi</a> — Converts a fixed point to integer with rounding.
<li><a href="#fixtorad_r">fixtorad_r</a> — Constant to convert angles in fixed point format to radians.
<li><a href="#ftofix">ftofix</a> — Converts a floating point value to fixed point.
<li><a href="#itofix">itofix</a> — Converts an integer to fixed point.
<li><a href="#radtofix_r">radtofix_r</a> — Constant to convert radians to fixed point angles.
</ul>
<p>
Allegro provides some routines for working with fixed point numbers, and
defines the type <tt>`fixed'</tt> to be a signed 32-bit integer. The high word is
used for the integer part and the low word for the fraction, giving a range
of -32768 to 32767 and an accuracy of about four or five decimal places.
Fixed point numbers can be assigned, compared, added, subtracted, negated and
shifted (for multiplying or dividing by powers of two) using the normal
integer operators, but you should take care to use the appropriate conversion
routines when mixing fixed point with integer or floating point values.
Writing `fixed_point_1 + fixed_point_2' is OK, but `fixed_point + integer' is
not.
<p>
Unfortunately the only advantage of fixed point math routines is that you
don't require a floating point coprocessor to use them. This was great in the
time period of i386 and i486 machines, but stopped being so useful with the
coming of the Pentium class of processors. From Pentium onwards, CPUs have
increased their strength in floating point operations, equaling or even
surpassing integer math performance.
<p>
Depending on the type of operations your program may need, using floating
point types may be faster than fixed types if you are targeting a specific
machine class. Allegro comes with a test program in the <tt>`allegro/tests'</tt>
directory. Its <tt>`Misc'</tt> menu contains a basic profile test which can give you
an idea of the speed difference between fixed and float types for a few basic
operations on your machine. However, don't forget to profile your program in
real life conditions, tight loop benchmarks are after all artificial.
<p>
Fixed point math is considered "add-on" material and is kept only for
backwards compatibility. Whenever a future release of Allegro breaks backwards
compatibility, fixed point math will likely be moved to a separate add-on
package for the very few users who still find it convenient and useful, and
Allegro functions using fixed point math will use other types.
<p><br>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="itofix">itofix</a>(int x);</b></div><br>
Converts an integer to fixed point. This is the same thing as x<<16.
Remember that overflows (trying to convert an integer greater than 32767)
and underflows (trying to convert an integer lesser than -32768) are not
detected even in debug builds! The values simply "wrap around". Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> number;
/* This conversion is OK. */
number = <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100);
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(<a href="#fixtoi" class="autotype" title="Converts a fixed point to integer with rounding.">fixtoi</a>(number) == 100);
number = <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(64000);
/* This check will fail in debug builds. */
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(<a href="#fixtoi" class="autotype" title="Converts a fixed point to integer with rounding.">fixtoi</a>(number) == 64000);</pre></blockquote>
<p><b>Return value:</b>
Returns the value of the integer converted to fixed point ignoring
overflows.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixtoi" title="Converts a fixed point to integer with rounding.">fixtoi</a>,
<a class="xref" href="#ftofix" title="Converts a floating point value to fixed point.">ftofix</a>,
<a class="xref" href="#fixtof" title="Converts a fixed point to floating point.">fixtof</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#ex12bit" title="How to fake a 12-bit truecolor mode on an 8-bit card.">ex12bit</a>,
<a class="eref" href="alleg045.html#ex3buf" title="Mode-X triple buffering and retrace interrupt simulation.">ex3buf</a>,
<a class="eref" href="alleg045.html#ex3d" title="3d 'bouncy cubes' demo.">ex3d</a>,
<a class="eref" href="alleg045.html#exblend" title="Using translucency in truecolor modes.">exblend</a>,
<a class="eref" href="alleg045.html#excustom" title="Creating custom GUI objects.">excustom</a>,
<a class="eref" href="alleg045.html#exfixed" title="Using fixed point maths.">exfixed</a>,
<a class="eref" href="alleg045.html#exlights" title="One way to do colored lighting effects in a hicolor video mode.">exlights</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>,
<a class="eref" href="alleg045.html#exsprite" title="Datafiles access and sprite animation.">exsprite</a>,
<a class="eref" href="alleg045.html#exstars" title="3d starfield and lightsourced spaceship.">exstars</a>.</blockquote>
<div class="al-api"><b>int <a name="fixtoi">fixtoi</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
Converts fixed point to integer, rounding as required to the nearest
integer. Example:
<blockquote class="code"><pre>
int result;
/* This will put 33 into <tt>`result'</tt>. */
result = <a href="#fixtoi" class="autotype" title="Converts a fixed point to integer with rounding.">fixtoi</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 3);
/* But this will round up to 17. */
result = <a href="#fixtoi" class="autotype" title="Converts a fixed point to integer with rounding.">fixtoi</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 6);</pre></blockquote>
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#itofix" title="Converts an integer to fixed point.">itofix</a>,
<a class="xref" href="#ftofix" title="Converts a floating point value to fixed point.">ftofix</a>,
<a class="xref" href="#fixtof" title="Converts a fixed point to floating point.">fixtof</a>,
<a class="xref" href="#fixfloor" title="Returns the greatest integer not greater than x.">fixfloor</a>,
<a class="xref" href="#fixceil" title="Returns the smallest integer not less than x.">fixceil</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#ex12bit" title="How to fake a 12-bit truecolor mode on an 8-bit card.">ex12bit</a>,
<a class="eref" href="alleg045.html#ex3buf" title="Mode-X triple buffering and retrace interrupt simulation.">ex3buf</a>,
<a class="eref" href="alleg045.html#ex3d" title="3d 'bouncy cubes' demo.">ex3d</a>,
<a class="eref" href="alleg045.html#exblend" title="Using translucency in truecolor modes.">exblend</a>,
<a class="eref" href="alleg045.html#excustom" title="Creating custom GUI objects.">excustom</a>,
<a class="eref" href="alleg045.html#exlights" title="One way to do colored lighting effects in a hicolor video mode.">exlights</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>,
<a class="eref" href="alleg045.html#exstars" title="3d starfield and lightsourced spaceship.">exstars</a>,
<a class="eref" href="alleg045.html#exupdate" title="Supporting different screen update methods in a single program.">exupdate</a>.</blockquote>
<div class="al-api"><b>int <a name="fixfloor">fixfloor</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
Returns the greatest integer not greater than x. That is, it rounds
towards negative infinity. Example:
<blockquote class="code"><pre>
int result;
/* This will put 33 into <tt>`result'</tt>. */
result = <a href="#fixfloor" class="autotype" title="Returns the greatest integer not greater than x.">fixfloor</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 3);
/* And this will round down to 16. */
result = <a href="#fixfloor" class="autotype" title="Returns the greatest integer not greater than x.">fixfloor</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 6);</pre></blockquote>
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixtoi" title="Converts a fixed point to integer with rounding.">fixtoi</a>,
<a class="xref" href="#fixceil" title="Returns the smallest integer not less than x.">fixceil</a>.</blockquote>
<div class="al-api"><b>int <a name="fixceil">fixceil</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
Returns the smallest integer not less than x. That is, it rounds towards
positive infinity. Example:
<blockquote class="code"><pre>
int result;
/* This will put 34 into <tt>`result'</tt>. */
result = <a href="#fixceil" class="autotype" title="Returns the smallest integer not less than x.">fixceil</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 3);
/* This will round up to 17. */
result = <a href="#fixceil" class="autotype" title="Returns the smallest integer not less than x.">fixceil</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 6);</pre></blockquote>
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixtoi" title="Converts a fixed point to integer with rounding.">fixtoi</a>,
<a class="xref" href="#fixfloor" title="Returns the greatest integer not greater than x.">fixfloor</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="ftofix">ftofix</a>(double x);</b></div><br>
Converts a floating point value to fixed point. Unlike itofix(), this
function clamps values which could overflow the type conversion, setting
<tt>`errno'</tt> to ERANGE in the process if this happens. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> number;
number = <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-40000);
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(<a href="#fixfloor" class="autotype" title="Returns the greatest integer not greater than x.">fixfloor</a>(number) == -32768);
number = <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(64000);
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(<a href="#fixfloor" class="autotype" title="Returns the greatest integer not greater than x.">fixfloor</a>(number) == 32767);
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(!errno); /* This will fail. */</pre></blockquote>
<p><b>Return value:</b>
Returns the value of the floating point value converted to fixed point
clamping overflows (and setting <tt>`errno'</tt>).
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixtof" title="Converts a fixed point to floating point.">fixtof</a>,
<a class="xref" href="#itofix" title="Converts an integer to fixed point.">itofix</a>,
<a class="xref" href="#fixtoi" title="Converts a fixed point to integer with rounding.">fixtoi</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exfixed" title="Using fixed point maths.">exfixed</a>,
<a class="eref" href="alleg045.html#exrotscl" title="Demonstrates rotate_scaled_sprite functions.">exrotscl</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>,
<a class="eref" href="alleg045.html#exupdate" title="Supporting different screen update methods in a single program.">exupdate</a>.</blockquote>
<div class="al-api"><b>double <a name="fixtof">fixtof</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
Converts fixed point to floating point. Example:
<blockquote class="code"><pre>
float result;
/* This will put 33.33333 into <tt>`result'</tt>. */
result = <a href="#fixtof" class="autotype" title="Converts a fixed point to floating point.">fixtof</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 3);
/* This will put 16.66666 into <tt>`result'</tt>. */
result = <a href="#fixtof" class="autotype" title="Converts a fixed point to floating point.">fixtof</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100) / 6);</pre></blockquote>
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#ftofix" title="Converts a floating point value to fixed point.">ftofix</a>,
<a class="xref" href="#itofix" title="Converts an integer to fixed point.">itofix</a>,
<a class="xref" href="#fixtoi" title="Converts a fixed point to integer with rounding.">fixtoi</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exfixed" title="Using fixed point maths.">exfixed</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>,
<a class="eref" href="alleg045.html#exstars" title="3d starfield and lightsourced spaceship.">exstars</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixmul">fixmul</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x, <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> y);</b></div><br>
A fixed point value can be multiplied or divided by an integer with the
normal `*' and <tt>`/'</tt> operators. To multiply two fixed point values, though,
you must use this function.
<p>
If an overflow occurs, <tt>`errno'</tt> will be set and the maximum possible value
will be returned, but <tt>`errno'</tt> is not cleared if the operation is
successful. This means that if you are going to test for overflow you
should set <tt>`errno=0'</tt> before calling fixmul(). Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> result;
/* This will put 30000 into <tt>`result'</tt>. */
result = <a href="#fixmul" class="autotype" title="Multiplies two fixed point values together.">fixmul</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(10), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(3000));
/* But this overflows, and sets <tt>`errno'</tt>. */
result = <a href="#fixmul" class="autotype" title="Multiplies two fixed point values together.">fixmul</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(100), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(3000));
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(!errno);</pre></blockquote>
<p><b>Return value:</b>
Returns the clamped result of multiplying <tt>`x'</tt> by <tt>`y'</tt>, setting <tt>`errno'</tt> to
ERANGE if there was an overflow.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixadd" title="Safe function to add fixed point numbers clamping overflow.">fixadd</a>,
<a class="xref" href="#fixsub" title="Safe function to subtract fixed point numbers clamping underflow.">fixsub</a>,
<a class="xref" href="#fixdiv" title="Fixed point division.">fixdiv</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#ex3buf" title="Mode-X triple buffering and retrace interrupt simulation.">ex3buf</a>,
<a class="eref" href="alleg045.html#excustom" title="Creating custom GUI objects.">excustom</a>,
<a class="eref" href="alleg045.html#exfixed" title="Using fixed point maths.">exfixed</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>,
<a class="eref" href="alleg045.html#exstars" title="3d starfield and lightsourced spaceship.">exstars</a>,
<a class="eref" href="alleg045.html#exupdate" title="Supporting different screen update methods in a single program.">exupdate</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixdiv">fixdiv</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x, <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> y);</b></div><br>
A fixed point value can be divided by an integer with the normal <tt>`/'</tt>
operator. To divide two fixed point values, though, you must use this
function. If a division by zero occurs, <tt>`errno'</tt> will be set and the
maximum possible value will be returned, but <tt>`errno'</tt> is not cleared if the
operation is successful. This means that if you are going to test for
division by zero you should set <tt>`errno=0'</tt> before calling fixdiv(). Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> result;
/* This will put 0.06060 <tt>`result'</tt>. */
result = <a href="#fixdiv" class="autotype" title="Fixed point division.">fixdiv</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(2), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(33));
/* This will put 0 into <tt>`result'</tt>. */
result = <a href="#fixdiv" class="autotype" title="Fixed point division.">fixdiv</a>(0, <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-30));
/* Sets <tt>`errno'</tt> and puts -32768 into <tt>`result'</tt>. */
result = <a href="#fixdiv" class="autotype" title="Fixed point division.">fixdiv</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-100), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(0));
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(!errno); /* This will fail. */</pre></blockquote>
<p><b>Return value:</b>
Returns the result of dividing <tt>`x'</tt> by <tt>`y'</tt>. If <tt>`y'</tt> is zero, returns the
maximum possible fixed point value and sets <tt>`errno'</tt> to ERANGE.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixadd" title="Safe function to add fixed point numbers clamping overflow.">fixadd</a>,
<a class="xref" href="#fixsub" title="Safe function to subtract fixed point numbers clamping underflow.">fixsub</a>,
<a class="xref" href="#fixmul" title="Multiplies two fixed point values together.">fixmul</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exfixed" title="Using fixed point maths.">exfixed</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixadd">fixadd</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x, <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> y);</b></div><br>
Although fixed point numbers can be added with the normal '+' integer
operator, that doesn't provide any protection against overflow. If overflow
is a problem, you should use this function instead. It is slower than using
integer operators, but if an overflow occurs it will set <tt>`errno'</tt> and clamp
the result, rather than just letting it wrap. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> result;
/* This will put 5035 into <tt>`result'</tt>. */
result = <a href="#fixadd" class="autotype" title="Safe function to add fixed point numbers clamping overflow.">fixadd</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(5000), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(35));
/* Sets <tt>`errno'</tt> and puts -32768 into <tt>`result'</tt>. */
result = <a href="#fixadd" class="autotype" title="Safe function to add fixed point numbers clamping overflow.">fixadd</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-31000), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-3000));
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(!errno); /* This will fail. */</pre></blockquote>
<p><b>Return value:</b>
Returns the clamped result of adding <tt>`x'</tt> to <tt>`y'</tt>, setting <tt>`errno'</tt> to ERANGE
if there was an overflow.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixsub" title="Safe function to subtract fixed point numbers clamping underflow.">fixsub</a>,
<a class="xref" href="#fixmul" title="Multiplies two fixed point values together.">fixmul</a>,
<a class="xref" href="#fixdiv" title="Fixed point division.">fixdiv</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixsub">fixsub</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x, <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> y);</b></div><br>
Although fixed point numbers can be subtracted with the normal '-' integer
operator, that doesn't provide any protection against overflow. If overflow
is a problem, you should use this function instead. It is slower than using
integer operators, but if an overflow occurs it will set <tt>`errno'</tt> and clamp
the result, rather than just letting it wrap. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> result;
/* This will put 4965 into <tt>`result'</tt>. */
result = <a href="#fixsub" class="autotype" title="Safe function to subtract fixed point numbers clamping underflow.">fixsub</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(5000), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(35));
/* Sets <tt>`errno'</tt> and puts -32768 into <tt>`result'</tt>. */
result = <a href="#fixsub" class="autotype" title="Safe function to subtract fixed point numbers clamping underflow.">fixsub</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-31000), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(3000));
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(!errno); /* This will fail. */</pre></blockquote>
<p><b>Return value:</b>
Returns the clamped result of subtracting <tt>`y'</tt> from <tt>`x'</tt>, setting <tt>`errno'</tt> to
ERANGE if there was an overflow.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixadd" title="Safe function to add fixed point numbers clamping overflow.">fixadd</a>,
<a class="xref" href="#fixmul" title="Multiplies two fixed point values together.">fixmul</a>,
<a class="xref" href="#fixdiv" title="Fixed point division.">fixdiv</a>.</blockquote>
<br><center><h2><a name="Fixed point trig">Fixed point trig</a></h2></center><p>
The fixed point square root, sin, cos, tan, inverse sin, and inverse cos
functions are implemented using lookup tables, which are very fast but not
particularly accurate. At the moment the inverse tan uses an iterative
search on the tan table, so it is a lot slower than the others. Note that on
machines with very good floating point processors using these functions could
be slower in real life code due to cache misses: it may be faster to wait
a few extra cycles for a floating point sine result rather than wait for the
CPU to fetch the precalculated table from main memory. Always profile your
code.
<p>
Angles are represented in a binary format with 256 equal to a full circle,
64 being a right angle and so on. This has the advantage that a simple
bitwise 'and' can be used to keep the angle within the range zero to a full
circle, eliminating all those tiresome 'if (angle >= 360)' checks.
<p><br>
<div class="al-api"><b>extern const <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixtorad_r">fixtorad_r</a>;</b></div><br>
This constant gives a ratio which can be used to convert a fixed point
number in binary angle format to a fixed point number in radians. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> rad_angle, binary_angle;
/* Set the binary angle to 90 degrees. */
binary_angle = 64;
/* Now convert to radians (about 1.57). */
rad_angle = <a href="#fixmul" class="autotype" title="Multiplies two fixed point values together.">fixmul</a>(binary_angle, <a href="#fixtorad_r" class="autotype" title="Constant to convert angles in fixed point format to radians.">fixtorad_r</a>);</pre></blockquote>
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixmul" title="Multiplies two fixed point values together.">fixmul</a>,
<a class="xref" href="#radtofix_r" title="Constant to convert radians to fixed point angles.">radtofix_r</a>.</blockquote>
<div class="al-api"><b>extern const <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="radtofix_r">radtofix_r</a>;</b></div><br>
This constant gives a ratio which can be used to convert a fixed point
number in radians to a fixed point number in binary angle format. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> rad_angle, binary_angle;
...
binary_angle = <a href="#fixmul" class="autotype" title="Multiplies two fixed point values together.">fixmul</a>(rad_angle, <a href="#radtofix_r" class="autotype" title="Constant to convert radians to fixed point angles.">radtofix_r</a>);</pre></blockquote>
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#fixmul" title="Multiplies two fixed point values together.">fixmul</a>,
<a class="xref" href="#fixtorad_r" title="Constant to convert angles in fixed point format to radians.">fixtorad_r</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixsin">fixsin</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This function finds the sine of a value using a lookup table. The input
value must be a fixed point binary angle. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> angle;
int result;
/* Set the binary angle to 90 degrees. */
angle = <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(64);
/* The sine of 90 degrees is one. */
result = <a href="#fixtoi" class="autotype" title="Converts a fixed point to integer with rounding.">fixtoi</a>(<a href="#fixsin" class="autotype" title="Fixed point sine of binary angles.">fixsin</a>(angle));
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(result == 1);</pre></blockquote>
<p><b>Return value:</b>
Returns the sine of a fixed point binary format angle. The return value
will be in radians.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#ex12bit" title="How to fake a 12-bit truecolor mode on an 8-bit card.">ex12bit</a>,
<a class="eref" href="alleg045.html#ex3buf" title="Mode-X triple buffering and retrace interrupt simulation.">ex3buf</a>,
<a class="eref" href="alleg045.html#exblend" title="Using translucency in truecolor modes.">exblend</a>,
<a class="eref" href="alleg045.html#excustom" title="Creating custom GUI objects.">excustom</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>,
<a class="eref" href="alleg045.html#exupdate" title="Supporting different screen update methods in a single program.">exupdate</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixcos">fixcos</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This function finds the cosine of a value using a lookup table. The input
value must be a fixed point binary angle. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> angle;
float result;
/* Set the binary angle to 45 degrees. */
angle = <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(32);
/* The cosine of 45 degrees is about 0.7071. */
result = <a href="#fixtof" class="autotype" title="Converts a fixed point to floating point.">fixtof</a>(<a href="#fixcos" class="autotype" title="Fixed point cosine of binary angles.">fixcos</a>(angle));
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(result > 0.7 && result < 0.71);</pre></blockquote>
<p><b>Return value:</b>
Returns the cosine of a fixed point binary format angle. The return value
will be in radians.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#ex12bit" title="How to fake a 12-bit truecolor mode on an 8-bit card.">ex12bit</a>,
<a class="eref" href="alleg045.html#ex3buf" title="Mode-X triple buffering and retrace interrupt simulation.">ex3buf</a>,
<a class="eref" href="alleg045.html#exblend" title="Using translucency in truecolor modes.">exblend</a>,
<a class="eref" href="alleg045.html#excustom" title="Creating custom GUI objects.">excustom</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>,
<a class="eref" href="alleg045.html#exupdate" title="Supporting different screen update methods in a single program.">exupdate</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixtan">fixtan</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This function finds the tangent of a value using a lookup table. The input
value must be a fixed point binary angle. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> angle, res_a, res_b;
float dif;
angle = <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(37);
/* Prove that tan(angle) == sin(angle) / cos(angle). */
res_a = <a href="#fixdiv" class="autotype" title="Fixed point division.">fixdiv</a>(<a href="#fixsin" class="autotype" title="Fixed point sine of binary angles.">fixsin</a>(angle), <a href="#fixcos" class="autotype" title="Fixed point cosine of binary angles.">fixcos</a>(angle));
res_b = <a href="#fixtan" class="autotype" title="Fixed point tangent of binary angles.">fixtan</a>(angle);
dif = <a href="#fixtof" class="autotype" title="Converts a fixed point to floating point.">fixtof</a>(<a href="#fixsub" class="autotype" title="Safe function to subtract fixed point numbers clamping underflow.">fixsub</a>(res_a, res_b));
<a href="alleg000.html#allegro_message" class="autotype" title="Used mainly to show error messages to users.">allegro_message</a>("Precision error: %f\n", dif);</pre></blockquote>
<p><b>Return value:</b>
Returns the tangent of a fixed point binary format angle. The return value
will be in radians.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixasin">fixasin</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This function finds the inverse sine of a value using a lookup table. The
input value must be a fixed point value. The inverse sine is defined only
in the domain from `-1' to <tt>`1'</tt>. Outside of this input range, the function
will set <tt>`errno'</tt> to EDOM and return zero. Example:
<blockquote class="code"><pre>
float angle;
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> val;
/* Sets <tt>`val'</tt> to a right binary angle (<tt>`64'</tt>). */
val = <a href="#fixasin" class="autotype" title="Fixed point inverse sine lookup table.">fixasin</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(1));
/* Sets <tt>`angle'</tt> to 0.2405. */
angle = <a href="#fixtof" class="autotype" title="Converts a fixed point to floating point.">fixtof</a>(<a href="#fixmul" class="autotype" title="Multiplies two fixed point values together.">fixmul</a>(<a href="#fixasin" class="autotype" title="Fixed point inverse sine lookup table.">fixasin</a>(<a href="#ftofix" class="autotype" title="Converts a floating point value to fixed point.">ftofix</a>(0.238)), <a href="#fixtorad_r" class="autotype" title="Constant to convert angles in fixed point format to radians.">fixtorad_r</a>));
/* This will trigger the assert. */
val = <a href="#fixasin" class="autotype" title="Fixed point inverse sine lookup table.">fixasin</a>(<a href="#ftofix" class="autotype" title="Converts a floating point value to fixed point.">ftofix</a>(-1.09));
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(!errno);</pre></blockquote>
<p><b>Return value:</b>
Returns the inverse sine of a fixed point value, measured as fixed point
binary format angle, or zero if the input was out of the range. All return
values of this function will be in the range `-64' to <tt>`64'</tt>.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixacos">fixacos</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This function finds the inverse cosine of a value using a lookup table. The
input value must be a fixed point radian. The inverse cosine is defined
only in the domain from `-1' to <tt>`1'</tt>. Outside of this input range, the
function will set <tt>`errno'</tt> to EDOM and return zero. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> result;
/* Sets <tt>`result'</tt> to binary angle 128. */
result = <a href="#fixacos" class="autotype" title="Fixed point inverse cosine lookup table.">fixacos</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-1));</pre></blockquote>
<p><b>Return value:</b>
Returns the inverse sine of a fixed point value, measured as fixed point
binary format angle, or zero if the input was out of range. All return
values of this function will be in the range <tt>`0'</tt> to <tt>`128'</tt>.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixatan">fixatan</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This function finds the inverse tangent of a value using a lookup table.
The input value must be a fixed point radian. The inverse tangent is the
value whose tangent is <tt>`x'</tt>. Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> result;
/* Sets <tt>`result'</tt> to binary angle 13. */
result = <a href="#fixatan" class="autotype" title="Fixed point inverse tangent lookup table.">fixatan</a>(<a href="#ftofix" class="autotype" title="Converts a floating point value to fixed point.">ftofix</a>(0.326));</pre></blockquote>
<p><b>Return value:</b>
Returns the inverse tangent of a fixed point value, measured as a fixed
point binary format angle.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixatan2">fixatan2</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> y, <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This is a fixed point version of the libc atan2() routine. It computes the
arc tangent of `y / x', but the signs of both arguments are used to
determine the quadrant of the result, and <tt>`x'</tt> is permitted to be zero. This
function is useful to convert Cartesian coordinates to polar coordinates.
Example:
<blockquote class="code"><pre>
<a href="alleg001.html#fixed" class="autotype" title="Fixed point integer to replace floats.">fixed</a> result;
/* Sets <tt>`result'</tt> to binary angle 64. */
result = <a href="#fixatan2" class="autotype" title="Fixed point version of the libc atan2() routine.">fixatan2</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(1), 0);
/* Sets <tt>`result'</tt> to binary angle -109. */
result = <a href="#fixatan2" class="autotype" title="Fixed point version of the libc atan2() routine.">fixatan2</a>(<a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-1), <a href="#itofix" class="autotype" title="Converts an integer to fixed point.">itofix</a>(-2));
/* Fails the assert. */
result = <a href="#fixatan2" class="autotype" title="Fixed point version of the libc atan2() routine.">fixatan2</a>(0, 0);
<a href="alleg044.html#ASSERT" class="autotype" title="Debugging helper macro to assert.">ASSERT</a>(!errno);</pre></blockquote>
<p><b>Return value:</b>
Returns the arc tangent of `y / x' in fixed point binary format angle,
from `-128' to <tt>`128'</tt>. If both <tt>`x'</tt> and <tt>`y'</tt> are zero, returns zero and sets
<tt>`errno'</tt> to EDOM.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exlights" title="One way to do colored lighting effects in a hicolor video mode.">exlights</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixsqrt">fixsqrt</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x);</b></div><br>
This finds out the non negative square root of <tt>`x'</tt>. If <tt>`x'</tt> is negative,
<tt>`errno'</tt> is set to EDOM and the function returns zero.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exfixed" title="Using fixed point maths.">exfixed</a>,
<a class="eref" href="alleg045.html#exlights" title="One way to do colored lighting effects in a hicolor video mode.">exlights</a>,
<a class="eref" href="alleg045.html#exspline" title="Constructing smooth movement paths from spline curves.">exspline</a>.</blockquote>
<div class="al-api"><b><a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> <a name="fixhypot">fixhypot</a>(<a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> x, <a class="autotype" href="alleg001.html#fixed" title="Fixed point integer to replace floats.">fixed</a> y);</b></div><br>
Fixed point hypotenuse (returns the square root of `x*x + y*y'). This
should be better than calculating the formula yourself manually, since
the error is much smaller.
<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#Fixed point trig" title="">Fixed point trig</a>.</blockquote>
<br><center><h2><a name="Fix class">Fix class</a></h2></center><p>
If you are programming in C++ you can ignore all the above and use the fix
class instead, which overloads a lot of operators to provide automatic
conversion to and from integer and floating point values, and calls the
above routines as they are required. You should not mix the fix class with
the fixed typedef though, because the compiler will mistake the fixed values
for regular integers and insert unnecessary conversions. For example, if x
is an object of class fix, calling fixsqrt(x) will return the wrong result.
You should use the overloaded sqrt(x) or x.sqrt() instead.
<p>
On top of that, the Fix class may be slower than using directly the C
functions because of implicit internal conversions from one type to another
which you otherwise could avoid or minimise. Finally, this is the only bit
of C++ in the whole Allegro library, and the developers are certainly going
to move it into add-on space in the next version of Allegro which breaks
source backwards compatibility.
<p><br>
<hr><div class="al-back-to-contents"><a href="allegro.html">Back to contents</a></div>
</body>
</html>
|