a*2
2 楼
求int sqrt(int x)越界如何处理啊?
看到以前的一个帖子,觉得好像不太对, INT_MAX是2^31-1不是2^32,用(1<<16)还是
会越界,请问下该如何处理?
还有判断越界一般用什么方法?多谢了
看到以前的一个帖子,觉得好像不太对, INT_MAX是2^31-1不是2^32,用(1<<16)还是
会越界,请问下该如何处理?
还有判断越界一般用什么方法?多谢了
d*g
3 楼
两个6TB的硬盘,不打算做RAID。存文档和电影。希望文档能在两个硬盘各存一份,但
是电影就无所谓。请问各位在用黑白DSM的是怎么创建volume或者disk group的?谢谢
!
是电影就无所谓。请问各位在用黑白DSM的是怎么创建volume或者disk group的?谢谢
!
a*2
4 楼
自己顶一下
a*a
5 楼
两个硬盘折腾什么?
做过cron job,用rsync定时备份就是了。
不过在同一台机器上备份,不安全哟。得有份冷备份。
做过cron job,用rsync定时备份就是了。
不过在同一台机器上备份,不安全哟。得有份冷备份。
i*e
6 楼
Good observation.
You are correct, my previous post has bugs and did not address the overflow
issue properly.
Two methods to address:
i) find the upper bound of SQRT(INT_MAX) once and pass it into that function
. The upper bound can be found using a helper function that tests for
overflow. (ie, as soon as the condition when x > x*x is met, we knew x-1 is
the upper bound).
ii) declare the parameter x as unsigned int. Then initialize the upper bound
,
hi = min(n/2, (1<<16) - 1);
Hope this helps.
【在 a**********2 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 求int sqrt(int x)越界如何处理啊?
:
: 看到以前的一个帖子,觉得好像不太对, INT_MAX是2^31-1不是2^32,用(1<<16)还是
: 会越界,请问下该如何处理?
: 还有判断越界一般用什么方法?多谢了
You are correct, my previous post has bugs and did not address the overflow
issue properly.
Two methods to address:
i) find the upper bound of SQRT(INT_MAX) once and pass it into that function
. The upper bound can be found using a helper function that tests for
overflow. (ie, as soon as the condition when x > x*x is met, we knew x-1 is
the upper bound).
ii) declare the parameter x as unsigned int. Then initialize the upper bound
,
hi = min(n/2, (1<<16) - 1);
Hope this helps.
【在 a**********2 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 求int sqrt(int x)越界如何处理啊?
:
: 看到以前的一个帖子,觉得好像不太对, INT_MAX是2^31-1不是2^32,用(1<<16)还是
: 会越界,请问下该如何处理?
: 还有判断越界一般用什么方法?多谢了
i*e
8 楼
This should also work too :)
The observation is when it overflows, it must be the case M > M*M, so
readjust the upper bound to M-1 when that happens.
int mysqrt(int n) {
int L = 0;
int H = min(n/2, (1<<16));
while (L < H) {
int M = L+(H-L+1)/2;
int M_sqr = M*M;
if (M <= M_sqr && M_sqr <= n)
L = M;
else /* M > M_sqr || (M_sqr > n) */
H = M-1;
}
return L;
}
The observation is when it overflows, it must be the case M > M*M, so
readjust the upper bound to M-1 when that happens.
int mysqrt(int n) {
int L = 0;
int H = min(n/2, (1<<16));
while (L < H) {
int M = L+(H-L+1)/2;
int M_sqr = M*M;
if (M <= M_sqr && M_sqr <= n)
L = M;
else /* M > M_sqr || (M_sqr > n) */
H = M-1;
}
return L;
}
k*j
9 楼
or just test if M*M<0, if so then it overflows.
【在 i**********e 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: This should also work too :)
: The observation is when it overflows, it must be the case M > M*M, so
: readjust the upper bound to M-1 when that happens.
: int mysqrt(int n) {
: int L = 0;
: int H = min(n/2, (1<<16));
: while (L < H) {
: int M = L+(H-L+1)/2;
: int M_sqr = M*M;
: if (M <= M_sqr && M_sqr <= n)
【在 i**********e 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: This should also work too :)
: The observation is when it overflows, it must be the case M > M*M, so
: readjust the upper bound to M-1 when that happens.
: int mysqrt(int n) {
: int L = 0;
: int H = min(n/2, (1<<16));
: while (L < H) {
: int M = L+(H-L+1)/2;
: int M_sqr = M*M;
: if (M <= M_sqr && M_sqr <= n)
相关阅读
Black friday 有啥好的硬件deal? 值得早上起来排队吗?借人气问下无线路由这个NAS怎莫样?这个显示器是什么问题?郁闷阿,插两条ram就蓝屏自己攒的机,为什么突然开机画面变成不是全屏了?问个多显设置的问题HP LAPTOP开机就关机问题有无线路由还需要modem连DNS网络吗?主板要求DDR-1066,能用DDR2-800内存吗?有人要AMD X4 965 + Gigabyte combo吗?成功的在ASUS WL-520GU上刷了tomato天气凉了请教配机器问题买到bb开苞的或准备自用的,我准备把全部未必都给你了。 (转载)Dell Inspiron 530换机箱的话SSD求推荐LCD monitor 和 HDTV monitor 有什么区别?【江湖救急】关于HAF 922的风扇连接P7P55d主板的问题有用W700DS的莫?