avatar
有没有四舍无入的函数?# Unix - 噫吁兮,危乎高哉
d*r
1
我知道int是取整,有没有类似的取整函数但是算法是四舍五入的?
avatar
y*y
2
why not write by yourself? it's so simple.

【在 d******r 的大作中提到】
: 我知道int是取整,有没有类似的取整函数但是算法是四舍五入的?
avatar
l*l
3
int(x+0.5)

【在 y***y 的大作中提到】
: why not write by yourself? it's so simple.
avatar
y*y
4
what if x is equal to interger.

【在 l*l 的大作中提到】
: int(x+0.5)
avatar
bz
5

no, lib functions are all doing round off, but writing one is very
easy.

【在 d******r 的大作中提到】
: 我知道int是取整,有没有类似的取整函数但是算法是四舍五入的?
avatar
h*h
6
take it easy, think about so many non-cs majors changed to
Computer Science.
It is always hard to get started.

【在 l*l 的大作中提到】
: int(x+0.5)
avatar
y*y
7
He deleted his post because he know he made a mistake.
BUt it seems you still dont understand and self-feeling good.
What I mean is int(x+0.5) gives you x if x is an interger itself.
but 四舍五入 gives you x+1. so int(x+0.5) is a wrong answer. You need
to write if-else.

【在 h**h 的大作中提到】
: take it easy, think about so many non-cs majors changed to
: Computer Science.
: It is always hard to get started.

avatar
y*y
8
faint...it seems I made the mistake....anyway,sometimes brain
stalk....:(

【在 y***y 的大作中提到】
: He deleted his post because he know he made a mistake.
: BUt it seems you still dont understand and self-feeling good.
: What I mean is int(x+0.5) gives you x if x is an interger itself.
: but 四舍五入 gives you x+1. so int(x+0.5) is a wrong answer. You need
: to write if-else.

avatar
y*y
9
okay, I made a mistake, but the int(x+0.5) is still a wrong answer.
hohohoho~

【在 y***y 的大作中提到】
: faint...it seems I made the mistake....anyway,sometimes brain
: stalk....:(

avatar
y*y
10
try some negative digits.
you still need to write if-else.

【在 y***y 的大作中提到】
: okay, I made a mistake, but the int(x+0.5) is still a wrong answer.
: hohohoho~

avatar
y*y
11
正解:
#!/usr/local/perl
$x=;
if($x>=0){
print int($x+0.5);
}
else{
print int($x-0.5);}
win!! hohohoho~

【在 y***y 的大作中提到】
: try some negative digits.
: you still need to write if-else.

avatar
p*h
12
how about this:
int i;
double d;
/* d = whatever number. */
i = (int)floor(d + 0.5);

【在 y***y 的大作中提到】
: 正解:
: #!/usr/local/perl
: $x=;
: if($x>=0){
: print int($x+0.5);
: }
: else{
: print int($x-0.5);}
: win!! hohohoho~

avatar
c*t
13
Sigh, you are the confused one :)
For the negative numbers, there are two rounding schemes.

【在 y***y 的大作中提到】
: 正解:
: #!/usr/local/perl
: $x=;
: if($x>=0){
: print int($x+0.5);
: }
: else{
: print int($x-0.5);}
: win!! hohohoho~

avatar
m*e
14
Mod maths treats it the same no matter you are + or -. Always the largest
int that is no larger than the number. This is consistent.

【在 c*****t 的大作中提到】
: Sigh, you are the confused one :)
: For the negative numbers, there are two rounding schemes.

avatar
l*l
15
you are smart, but the code is too long, even it is sample.
I write one line and solve it, I guess it is the best code any one can
get.
print int(($x+abs($x))/2+0.5)+int(($x-abs($x))/2-0.5);

【在 y***y 的大作中提到】
: try some negative digits.
: you still need to write if-else.

avatar
b*d
16
Best? How do you evaluate if a program is good or poor?
Do you think such code is in good style?

【在 l*l 的大作中提到】
: you are smart, but the code is too long, even it is sample.
: I write one line and solve it, I guess it is the best code any one can
: get.
: print int(($x+abs($x))/2+0.5)+int(($x-abs($x))/2-0.5);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。