由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - round function in math
相关主题
问个double和long double的问题来来来,我也问个题 (转载)
两个古董的C优化代码,现在还有效果么?一个小问题
今天被一个面试问题难住了《Intel® 64 and IA-32体系结构:软件开发人员手册》文字版[PDF]
double转换int的问题int这种类型的存在意义是什么?
关于Dword 和 word芯片應該為軟件服務,譬如硬件加速的虛擬內存MMU、虛擬GPU、虛
请问一个多线程与volatile关键字的问题。C 多线程的一个问题
请问:Auto_Ptr、Smart Ptr 和 Reference Counting是什么关系? (转载)VC++里一个函数有参数[out]LPWSTR p要不要初始化?
谁能示范一个小的C程序请教一个c语言实现多线程的问题
相关话题的讨论汇总
话题: round话题: function话题: double话题: ebp话题: nearest
进入Programming版参与讨论
1 (共1页)
q****4
发帖数: 1
1
hi,
any round function or similar things in C/C++?
such as round(5.5) = 6; round(6.4) =6...
thanks,
P********e
发帖数: 2610
2
inline int round(double r)
{
if (r > 0)
return (int)(r+0.5);
else return (int)(r-0.5);
}

【在 q****4 的大作中提到】
: hi,
: any round function or similar things in C/C++?
: such as round(5.5) = 6; round(6.4) =6...
: thanks,

d*****a
发帖数: 110
3
ROUND(3) Linux Programmer's Manual ROUND(
3)
NAME
round, roundf, roundl - round to nearest integer, away from zero
SYNOPSIS
#include
double round(double x);
float roundf(float x);
long double roundl(long double x);
DESCRIPTION
These functions round x to the nearest integer, but round halfway
cases
away from zero (regardless of the current rounding direction),
instead
of to the nearest even intege
t****t
发帖数: 6806
4
你不能用floor(r+0.5)么?

【在 P********e 的大作中提到】
: inline int round(double r)
: {
: if (r > 0)
: return (int)(r+0.5);
: else return (int)(r-0.5);
: }

P********e
发帖数: 2610
5
也可以,你这个调用cmath了,速度肯定慢,如果是大量数学操作,差别挺大的

【在 t****t 的大作中提到】
: 你不能用floor(r+0.5)么?
t****t
发帖数: 6806
6
你还真以为编译器现在落后到这种程度么.

【在 P********e 的大作中提到】
: 也可以,你这个调用cmath了,速度肯定慢,如果是大量数学操作,差别挺大的
P********e
发帖数: 2610
7
个人感觉吧,也可能是错的
不过我反正基本能不用cmath就不用

【在 t****t 的大作中提到】
: 你还真以为编译器现在落后到这种程度么.
z******i
发帖数: 59
8
This code might be very slow.
It will be translated to something like the following assemly code.
fldcw [ebp-4] ;Write new value to FCW
fistp DWORD PTR [ebp-16] ;Convert fvalue to integer
;and pop stack
fldcw [ebp-12] ;Restore FCW to its
;original state
It is a serial code in CPU because it changes FPU's control word.
A better solution is using SSE, 4 float one time

【在 P********e 的大作中提到】
: inline int round(double r)
: {
: if (r > 0)
: return (int)(r+0.5);
: else return (int)(r-0.5);
: }

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个c语言实现多线程的问题关于Dword 和 word
How to measure CPU time spent on a code block (with C or C++)?请问一个多线程与volatile关键字的问题。
为什么我的Navigate2总是崩溃在错误的参数表版本里?请问:Auto_Ptr、Smart Ptr 和 Reference Counting是什么关系? (转载)
Ruby 为什么输给了py谁能示范一个小的C程序
问个double和long double的问题来来来,我也问个题 (转载)
两个古董的C优化代码,现在还有效果么?一个小问题
今天被一个面试问题难住了《Intel® 64 and IA-32体系结构:软件开发人员手册》文字版[PDF]
double转换int的问题int这种类型的存在意义是什么?
相关话题的讨论汇总
话题: round话题: function话题: double话题: ebp话题: nearest