File: bomath.cpp

package info (click to toggle)
boson 0.13-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 12,992 kB
  • ctags: 29,287
  • sloc: cpp: 203,902; ansic: 9,959; makefile: 75; sh: 64; sed: 47
file content (60 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download | duplicates (2)
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
/*
    This file is part of the Boson game
    Copyright (C) 2004-2005 Andreas Beckermann (b_mann@gmx.de)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

// note the copyright above: this is LGPL!

#include "math/bomath.h"

#include <stdio.h>

void floatToBin(float _f)
{
 printf("%f: ", _f);
 int f = *((int*)&_f);
 for (int i = 31; i >= 0; i--) {
	if (f & (0x1 << i)) {
		printf("1");
	} else {
		printf("0");
	}
 }
 int sign = (f & (0x1 << 31)) ? 1 : 0;
 int e = 0;
 float m = 0;
 for (int i = 30; i >= 23; i--) {
	e <<= 1;
	if (f & (0x1 << i)) {
		e += 1;
	}
 }
 for (int i = 0; i < 23; i++) {
	m /= 2.0f;
	if (f & (0x1 << i)) {
		m += (0.5f);
	}
 }

 // ieee stuff. mantissa always has a leading 1, exponent has a bias of 127
 m += 1.0f;
 e -= 127;
 printf(" - sign=%d, e=%d, m=%f", sign, e, m);
 printf("\n");
}