1 /*****************************************************************************/
2 // Copyright 2006-2019 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 #include "dng_rational.h"
10 
11 #include "dng_utils.h"
12 
13 /*****************************************************************************/
14 
As_real64() const15 real64 dng_srational::As_real64 () const
16 	{
17 
18 	if (d)
19 		return (real64) n / (real64) d;
20 
21 	else
22 		return 0.0;
23 
24 	}
25 
26 /*****************************************************************************/
27 
Set_real64(real64 x,int32 dd)28 void dng_srational::Set_real64 (real64 x, int32 dd)
29 	{
30 
31 	if (x == 0.0)
32 		{
33 
34 		*this = dng_srational (0, 1);
35 
36 		}
37 
38 	if (dd == 0)
39 		{
40 
41 		real64 y = Abs_real64 (x);
42 
43 		if (y >= 32768.0)
44 			{
45 			dd = 1;
46 			}
47 
48 		else if (y >= 1.0)
49 			{
50 			dd = 32768;
51 			}
52 
53 		else
54 			{
55 			dd = 32768 * 32768;
56 			}
57 
58 		}
59 
60 	*this = dng_srational (Round_int32 (x * dd), dd);
61 
62 	}
63 
64 /*****************************************************************************/
65 
ReduceByFactor(int32 factor)66 void dng_srational::ReduceByFactor (int32 factor)
67 	{
68 
69 	while (n % factor == 0 &&
70 		   d % factor == 0 &&
71 		   d >= factor)
72 		{
73 		n /= factor;
74 		d /= factor;
75 		}
76 
77 	}
78 
79 /*****************************************************************************/
80 
As_real64() const81 real64 dng_urational::As_real64 () const
82 	{
83 
84 	if (d)
85 		return (real64) n / (real64) d;
86 
87 	else
88 		return 0.0;
89 
90 	}
91 
92 /*****************************************************************************/
93 
Set_real64(real64 x,uint32 dd)94 void dng_urational::Set_real64 (real64 x, uint32 dd)
95 	{
96 
97 	if (x <= 0.0)
98 		{
99 
100 		*this = dng_urational (0, 1);
101 
102 		}
103 
104 	if (dd == 0)
105 		{
106 
107 		if (x >= 32768.0)
108 			{
109 			dd = 1;
110 			}
111 
112 		else if (x >= 1.0)
113 			{
114 			dd = 32768;
115 			}
116 
117 		else
118 			{
119 			dd = 32768 * 32768;
120 			}
121 
122 		}
123 
124 	*this = dng_urational (Round_uint32 (x * dd), dd);
125 
126 	}
127 
128 /*****************************************************************************/
129 
ReduceByFactor(uint32 factor)130 void dng_urational::ReduceByFactor (uint32 factor)
131 	{
132 
133 	while (n % factor == 0 &&
134 		   d % factor == 0 &&
135 		   d >= factor)
136 		{
137 		n /= factor;
138 		d /= factor;
139 		}
140 
141 	}
142 
143 /*****************************************************************************/
144