Author: ai0867
Date: Sat Dec 6 00:39:40 2008
New Revision: 31298
URL: http://svn.gna.org/viewcvs/wesnoth?rev=31298&view=rev
Log:
Fix round() and pow() issues reported on forum.
(http://www.wesnoth.org/forum/viewtopic.php?f=21&t=23172)
Modified:
trunk/src/game_events.cpp
trunk/src/util.hpp
Modified: trunk/src/game_events.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.cpp?rev=31298&r1=31297&r2=31298&view=diff
==============================================================================
--- trunk/src/game_events.cpp (original)
+++ trunk/src/game_events.cpp Sat Dec 6 00:39:40 2008
@@ -1102,18 +1102,18 @@
double value = lexical_cast<double>(var.c_str());
if (round_val == "ceil") {
//TODO precision stuff
- value = ceil(value);
+ value = std::ceil(value);
} else if (round_val == "floor") {
//TODO same
- value = floor(value);
+ value = std::floor(value);
} else {
// We assume the value is an integer.
// Any non-numerical values will be interpreted
as 0
// Which is probably what was intended anyway
const int decimals = atoi(round_val.c_str());
- value *= pow(10, decimals); //add $decimals
zeroes
- value = round(value);
- value *= pow(10, -decimals); //and remove them
+ value *= std::pow(10.0, decimals); //add
$decimals zeroes
+ value = round_portable(value); // round() isn't
implemented everywhere
+ value *= std::pow(10.0, -decimals); //and
remove them
}
var = str_cast(value);
}
Modified: trunk/src/util.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/util.hpp?rev=31298&r1=31297&r2=31298&view=diff
==============================================================================
--- trunk/src/util.hpp (original)
+++ trunk/src/util.hpp Sat Dec 6 00:39:40 2008
@@ -55,6 +55,11 @@
#else
return static_cast<int>((d >= 0.0)? std::floor(d + 0.5) : std::ceil(d -
0.5));
#endif
+}
+
+// Guaranteed to have portable results across different platforms
+inline double round_portable(double d) {
+ return (d >= 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5);
}
struct bad_lexical_cast {};
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits