1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com>
4  * Copyright (C) 2012-2021 MATE Developers
5  *
6  * Licensed under the GNU General Public License Version 2
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <glib.h>
27 
28 #include "egg-precision.h"
29 
30 /**
31  * egg_precision_round_up:
32  * @value: The input value
33  * @smallest: The smallest increment allowed
34  *
35  * 101, 10	110
36  * 95,  10	100
37  * 0,   10	0
38  * 112, 10	120
39  * 100, 10	100
40  **/
41 gint
egg_precision_round_up(gfloat value,gint smallest)42 egg_precision_round_up (gfloat value, gint smallest)
43 {
44 	gfloat division;
45 	if (fabs (value) < 0.01)
46 		return 0;
47 	if (smallest == 0) {
48 		g_warning ("divisor zero");
49 		return 0;
50 	}
51 	division = (gfloat) value / (gfloat) smallest;
52 	division = ceilf (division);
53 	division *= smallest;
54 	return (gint) division;
55 }
56 
57 /**
58  * egg_precision_round_down:
59  * @value: The input value
60  * @smallest: The smallest increment allowed
61  *
62  * 101, 10	100
63  * 95,  10	90
64  * 0,   10	0
65  * 112, 10	110
66  * 100, 10	100
67  **/
68 gint
egg_precision_round_down(gfloat value,gint smallest)69 egg_precision_round_down (gfloat value, gint smallest)
70 {
71 	gfloat division;
72 	if (fabs (value) < 0.01)
73 		return 0;
74 	if (smallest == 0) {
75 		g_warning ("divisor zero");
76 		return 0;
77 	}
78 	division = (gfloat) value / (gfloat) smallest;
79 	division = floorf (division);
80 	division *= smallest;
81 	return (gint) division;
82 }
83 
84 /***************************************************************************
85  ***                          MAKE CHECK TESTS                           ***
86  ***************************************************************************/
87 #ifdef EGG_TEST
88 #include "egg-test.h"
89 
90 void
egg_precision_test(gpointer data)91 egg_precision_test (gpointer data)
92 {
93 	guint value;
94 	EggTest *test = (EggTest *) data;
95 
96 	if (!egg_test_start (test, "EggPrecision"))
97 		return;
98 
99 	/************************************************************/
100 	egg_test_title (test, "limit precision down 0,10");
101 	value = egg_precision_round_down (0, 10);
102 	if (value == 0) {
103 		egg_test_success (test, "got %i", value);
104 	} else {
105 		egg_test_failed (test, "precision incorrect (%i)", value);
106 	}
107 
108 	/************************************************************/
109 	egg_test_title (test, "limit precision down 4,10");
110 	value = egg_precision_round_down (4, 10);
111 	if (value == 0) {
112 		egg_test_success (test, "got %i", value);
113 	} else {
114 		egg_test_failed (test, "precision incorrect (%i)", value);
115 	}
116 
117 	/************************************************************/
118 	egg_test_title (test, "limit precision down 11,10");
119 	value = egg_precision_round_down (11, 10);
120 	if (value == 10) {
121 		egg_test_success (test, "got %i", value);
122 	} else {
123 		egg_test_failed (test, "precision incorrect (%i)", value);
124 	}
125 
126 	/************************************************************/
127 	egg_test_title (test, "limit precision down 201,2");
128 	value = egg_precision_round_down (201, 2);
129 	if (value == 200) {
130 		egg_test_success (test, "got %i", value);
131 	} else {
132 		egg_test_failed (test, "precision incorrect (%i)", value);
133 	}
134 
135 	/************************************************************/
136 	egg_test_title (test, "limit precision down 100,10");
137 	value = egg_precision_round_down (100, 10);
138 	if (value == 100) {
139 		egg_test_success (test, "got %i", value);
140 	} else {
141 		egg_test_failed (test, "precision incorrect (%i)", value);
142 	}
143 
144 	/************************************************************/
145 	egg_test_title (test, "limit precision up 0,10");
146 	value = egg_precision_round_up (0, 10);
147 	if (value == 0) {
148 		egg_test_success (test, "got %i", value);
149 	} else {
150 		egg_test_failed (test, "precision incorrect (%i)", value);
151 	}
152 
153 	/************************************************************/
154 	egg_test_title (test, "limit precision up 4,10");
155 	value = egg_precision_round_up (4, 10);
156 	if (value == 10) {
157 		egg_test_success (test, "got %i", value);
158 	} else {
159 		egg_test_failed (test, "precision incorrect (%i)", value);
160 	}
161 
162 	/************************************************************/
163 	egg_test_title (test, "limit precision up 11,10");
164 	value = egg_precision_round_up (11, 10);
165 	if (value == 20) {
166 		egg_test_success (test, "got %i", value);
167 	} else {
168 		egg_test_failed (test, "precision incorrect (%i)", value);
169 	}
170 
171 	/************************************************************/
172 	egg_test_title (test, "limit precision up 201,2");
173 	value = egg_precision_round_up (201, 2);
174 	if (value == 202) {
175 		egg_test_success (test, "got %i", value);
176 	} else {
177 		egg_test_failed (test, "precision incorrect (%i)", value);
178 	}
179 
180 	/************************************************************/
181 	egg_test_title (test, "limit precision up 100,10");
182 	value = egg_precision_round_up (100, 10);
183 	if (value == 100) {
184 		egg_test_success (test, "got %i", value);
185 	} else {
186 		egg_test_failed (test, "precision incorrect (%i)", value);
187 	}
188 
189 	egg_test_end (test);
190 }
191 
192 #endif
193 
194