1/*****************************************************************************/
2// Copyright 2006 Adobe Systems Incorporated
3// All Rights Reserved.
4//
5// NOTICE:  Adobe permits you to use, modify, and distribute this file in
6// accordance with the terms of the Adobe license agreement accompanying it.
7/*****************************************************************************/
8
9/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_rational.cpp#1 $ */
10/* $DateTime: 2012/05/30 13:28:51 $ */
11/* $Change: 832332 $ */
12/* $Author: tknoll $ */
13
14/*****************************************************************************/
15
16#include "dng_rational.h"
17
18#include "dng_utils.h"
19
20/*****************************************************************************/
21
22real64 dng_srational::As_real64 () const
23	{
24
25	if (d)
26		return (real64) n / (real64) d;
27
28	else
29		return 0.0;
30
31	}
32
33/*****************************************************************************/
34
35void dng_srational::Set_real64 (real64 x, int32 dd)
36	{
37
38	if (x == 0.0)
39		{
40
41		*this = dng_srational (0, 1);
42
43		}
44
45	if (dd == 0)
46		{
47
48		real64 y = Abs_real64 (x);
49
50		if (y >= 32768.0)
51			{
52			dd = 1;
53			}
54
55		else if (y >= 1.0)
56			{
57			dd = 32768;
58			}
59
60		else
61			{
62			dd = 32768 * 32768;
63			}
64
65		}
66
67	*this = dng_srational (Round_int32 (x * dd), dd);
68
69	}
70
71/*****************************************************************************/
72
73void dng_srational::ReduceByFactor (int32 factor)
74	{
75
76	while (n % factor == 0 &&
77		   d % factor == 0 &&
78		   d >= factor)
79		{
80		n /= factor;
81		d /= factor;
82		}
83
84	}
85
86/*****************************************************************************/
87
88real64 dng_urational::As_real64 () const
89	{
90
91	if (d)
92		return (real64) n / (real64) d;
93
94	else
95		return 0.0;
96
97	}
98
99/*****************************************************************************/
100
101void dng_urational::Set_real64 (real64 x, uint32 dd)
102	{
103
104	if (x <= 0.0)
105		{
106
107		*this = dng_urational (0, 1);
108
109		}
110
111	if (dd == 0)
112		{
113
114		if (x >= 32768.0)
115			{
116			dd = 1;
117			}
118
119		else if (x >= 1.0)
120			{
121			dd = 32768;
122			}
123
124		else
125			{
126			dd = 32768 * 32768;
127			}
128
129		}
130
131	*this = dng_urational (Round_uint32 (x * dd), dd);
132
133	}
134
135/*****************************************************************************/
136
137void dng_urational::ReduceByFactor (uint32 factor)
138	{
139
140	while (n % factor == 0 &&
141		   d % factor == 0 &&
142		   d >= factor)
143		{
144		n /= factor;
145		d /= factor;
146		}
147
148	}
149
150/*****************************************************************************/
151