summaryrefslogtreecommitdiff
path: root/source/dng_rational.cpp
blob: 9cddf5bbd68225b19407cc1452262124a59a6976 (plain)
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
/*****************************************************************************/
// Copyright 2006 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/

/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_rational.cpp#1 $ */ 
/* $DateTime: 2012/05/30 13:28:51 $ */
/* $Change: 832332 $ */
/* $Author: tknoll $ */

/*****************************************************************************/

#include "dng_rational.h"

#include "dng_utils.h"

/*****************************************************************************/

real64 dng_srational::As_real64 () const
	{

	if (d)
		return (real64) n / (real64) d;

	else
		return 0.0;

	}

/*****************************************************************************/

void dng_srational::Set_real64 (real64 x, int32 dd)
	{
	
	if (x == 0.0)
		{
		
		*this = dng_srational (0, 1);

		}
	
	if (dd == 0)
		{
		
		real64 y = Abs_real64 (x);
	
		if (y >= 32768.0)
			{
			dd = 1;
			}
			
		else if (y >= 1.0)
			{
			dd = 32768;
			}
			
		else
			{
			dd = 32768 * 32768;
			}
			
		}
		
	*this = dng_srational (Round_int32 (x * dd), dd);

	}

/*****************************************************************************/

void dng_srational::ReduceByFactor (int32 factor)
	{
	
	while (n % factor == 0 &&
		   d % factor == 0 &&
		   d >= factor)
		{
		n /= factor;
		d /= factor;
		}
	
	}
		
/*****************************************************************************/

real64 dng_urational::As_real64 () const
	{

	if (d)
		return (real64) n / (real64) d;

	else
		return 0.0;

	}

/*****************************************************************************/

void dng_urational::Set_real64 (real64 x, uint32 dd)
	{
	
	if (x <= 0.0)
		{
		
		*this = dng_urational (0, 1);

		}
	
	if (dd == 0)
		{
	
		if (x >= 32768.0)
			{
			dd = 1;
			}
			
		else if (x >= 1.0)
			{
			dd = 32768;
			}
			
		else
			{
			dd = 32768 * 32768;
			}
			
		}
		
	*this = dng_urational (Round_uint32 (x * dd), dd);
	
	}
		
/*****************************************************************************/

void dng_urational::ReduceByFactor (uint32 factor)
	{
	
	while (n % factor == 0 &&
		   d % factor == 0 &&
		   d >= factor)
		{
		n /= factor;
		d /= factor;
		}
	
	}
		
/*****************************************************************************/