C++从零开始
回答
inline double myround(double x)
{
return x < 0 ? floor(x) : ceil(x);
}
如the article Huppie cites提到的,这是最好的表现为跨所有浮点类型作品的模板
见http://en.cppreference.com/w/cpp/numeric/math/floor和http://en.cppreference.com/w/cpp/numeric/math/floor
,或者由于大同,非功能版本:
x = (x < 0) ? floor(x) : ceil(x);
关于CPlusPlus.com上的类似问题有一篇不错的文章。最简单的解决问题的方法应该是这样的:
double customRound(double value) const {
return value < 0 ? floor(value) : ceil(value);
}
更好的解决办法是在文章中提到的,它使用一个模板:
//--------------------------------------------------------------------------
// symmetric round up
// Bias: away from zero
template <typename FloatType>
FloatType ceil0(const FloatType& value)
{
FloatType result = std::ceil(std::fabs(value));
return (value < 0.0) ? -result : result;
}
为什么不称为customFloor - 它既不是:P – 2009-08-25 07:22:35
我在引用的文章中看到它的内容为 template
有趣的文章,+1 –
2009-08-25 07:24:56
@Ruben Bartelink:'Round'应该更好:)... btw ..你有一些忍者打字技巧;-) – Huppie 2009-08-25 07:25:05
- 1. 从零开始
- 2. MVC从零开始
- 3. RDF从零开始
- 4. 从零开始的Lua/C++绑定
- 5. DJVU阅读器从零开始。 C#
- 6. 从零开始编程ARM C in
- 7. 从零开始学习YUI
- 8. 主从零开始查看
- 9. 从零开始学习PTX
- 10. 从零开始计数
- 11. UIScrollView和CATiledLayer从零开始
- 12. nvd3 multichart yaxis从零开始
- 13. 从零开始实现树
- 14. double:从零开始的roundaway
- 15. 从零开始的Modx idx
- 16. 建设从零开始
- 17. Java PRNG从零开始
- 18. rowindex始终从零开始......为什么?
- 19. iPhone开发环境(从零开始)
- 20. 开始从C#
- 21. 以零开始AUTO_INCREMENT
- 22. 开始,并从C#
- 23. 从C#开始CAD#
- 24. JavaScript告诉视频从一秒开始而不是零开始
- 25. NSWindow在清醒后从零开始
- 26. 从零开始构建dicom解析器
- 27. Rails:从零开始的令牌认证
- 28. 从零开始实施Redux商店
- 29. VB6:从零开始创建IHTMLElementCollection
- 30. 如何从零计数器verilog开始
从(原来的)标题为“C++双四舍五入“,我以为你的意思是四舍五入数字,只是为了确保:-) – paxdiablo 2009-08-25 07:26:35
你是对的:D thx编辑它 – Emre 2009-08-25 07:39:46