/src/dng_sdk/source/dng_rational.cpp
Line | Count | Source |
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 | | |
22 | | real64 dng_srational::As_real64 () const |
23 | 615k | { |
24 | | |
25 | 615k | if (d) |
26 | 610k | return (real64) n / (real64) d; |
27 | | |
28 | 5.38k | else |
29 | 5.38k | return 0.0; |
30 | | |
31 | 615k | } |
32 | | |
33 | | /*****************************************************************************/ |
34 | | |
35 | | void dng_srational::Set_real64 (real64 x, int32 dd) |
36 | 1.40M | { |
37 | | |
38 | 1.40M | if (x == 0.0) |
39 | 374k | { |
40 | | |
41 | 374k | *this = dng_srational (0, 1); |
42 | | |
43 | 374k | } |
44 | | |
45 | 1.40M | if (dd == 0) |
46 | 0 | { |
47 | | |
48 | 0 | real64 y = Abs_real64 (x); |
49 | | |
50 | 0 | if (y >= 32768.0) |
51 | 0 | { |
52 | 0 | dd = 1; |
53 | 0 | } |
54 | | |
55 | 0 | else if (y >= 1.0) |
56 | 0 | { |
57 | 0 | dd = 32768; |
58 | 0 | } |
59 | | |
60 | 0 | else |
61 | 0 | { |
62 | 0 | dd = 32768 * 32768; |
63 | 0 | } |
64 | | |
65 | 0 | } |
66 | | |
67 | 1.40M | *this = dng_srational (Round_int32 (x * dd), dd); |
68 | | |
69 | 1.40M | } |
70 | | |
71 | | /*****************************************************************************/ |
72 | | |
73 | | void dng_srational::ReduceByFactor (int32 factor) |
74 | 76.9k | { |
75 | | |
76 | 103k | while (n % factor == 0 && |
77 | 41.6k | d % factor == 0 && |
78 | 26.5k | d >= factor) |
79 | 26.5k | { |
80 | 26.5k | n /= factor; |
81 | 26.5k | d /= factor; |
82 | 26.5k | } |
83 | | |
84 | 76.9k | } |
85 | | |
86 | | /*****************************************************************************/ |
87 | | |
88 | | real64 dng_urational::As_real64 () const |
89 | 5.74M | { |
90 | | |
91 | 5.74M | if (d) |
92 | 5.68M | return (real64) n / (real64) d; |
93 | | |
94 | 62.8k | else |
95 | 62.8k | return 0.0; |
96 | | |
97 | 5.74M | } |
98 | | |
99 | | /*****************************************************************************/ |
100 | | |
101 | | void dng_urational::Set_real64 (real64 x, uint32 dd) |
102 | 570k | { |
103 | | |
104 | 570k | if (x <= 0.0) |
105 | 255k | { |
106 | | |
107 | 255k | *this = dng_urational (0, 1); |
108 | | |
109 | 255k | } |
110 | | |
111 | 570k | if (dd == 0) |
112 | 0 | { |
113 | | |
114 | 0 | if (x >= 32768.0) |
115 | 0 | { |
116 | 0 | dd = 1; |
117 | 0 | } |
118 | | |
119 | 0 | else if (x >= 1.0) |
120 | 0 | { |
121 | 0 | dd = 32768; |
122 | 0 | } |
123 | | |
124 | 0 | else |
125 | 0 | { |
126 | 0 | dd = 32768 * 32768; |
127 | 0 | } |
128 | | |
129 | 0 | } |
130 | | |
131 | 570k | *this = dng_urational (Round_uint32 (x * dd), dd); |
132 | | |
133 | 570k | } |
134 | | |
135 | | /*****************************************************************************/ |
136 | | |
137 | | void dng_urational::ReduceByFactor (uint32 factor) |
138 | 61.4k | { |
139 | | |
140 | 94.4k | while (n % factor == 0 && |
141 | 49.4k | d % factor == 0 && |
142 | 33.0k | d >= factor) |
143 | 33.0k | { |
144 | 33.0k | n /= factor; |
145 | 33.0k | d /= factor; |
146 | 33.0k | } |
147 | | |
148 | 61.4k | } |
149 | | |
150 | | /*****************************************************************************/ |