由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - 有没有四舍无入的函数?
相关主题
how to use a variable to represent interger which is bigger than 32768?what's wrong with this ftp batch file?
Re: how to use a variable to represent interger which is bigger than 3paste command 里面的 -
Sorry, my bad Re: how to use a variable to represent interger which is bigger than 3command line socket tools
Key in Shared Memoryunix下编译fortran 的错误太多,看不到怎么办?
CGI之Perl Script一问 How to capture all keystrokes in linux?
[转载] 请教Bash里面 <<< 是什么意思
help!!! pipe program[转载] how is pipe implemented in UNIX????
问一个关于at的问题[转载] shared library一问
相关话题的讨论汇总
话题: int话题: write话题: interger话题: print话题: mistake
进入Unix版参与讨论
1 (共1页)
d******r
发帖数: 4
1
我知道int是取整,有没有类似的取整函数但是算法是四舍五入的?
y***y
发帖数: 56
2
why not write by yourself? it's so simple.

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

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

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

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

【在 d******r 的大作中提到】
: 我知道int是取整,有没有类似的取整函数但是算法是四舍五入的?
h**h
发帖数: 132
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)
y***y
发帖数: 56
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.

y***y
发帖数: 56
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.

y***y
发帖数: 56
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....:(

y***y
发帖数: 56
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~

相关主题
[转载] 请教what's wrong with this ftp batch file?
help!!! pipe programpaste command 里面的 -
问一个关于at的问题command line socket tools
进入Unix版参与讨论
y***y
发帖数: 56
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.

p**h
发帖数: 99
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~

c*****t
发帖数: 1879
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~

m*****e
发帖数: 4193
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.

l*l
发帖数: 225
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.

b******d
发帖数: 28
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);

1 (共1页)
进入Unix版参与讨论
相关主题
[转载] shared library一问CGI之Perl Script一问
[转载] 再问一个shared library的问题[转载] 请教
请问在unix下如何看一个程序跑了多少时间?谢谢!help!!! pipe program
如何生成静态连接库?(zz)问一个关于at的问题
how to use a variable to represent interger which is bigger than 32768?what's wrong with this ftp batch file?
Re: how to use a variable to represent interger which is bigger than 3paste command 里面的 -
Sorry, my bad Re: how to use a variable to represent interger which is bigger than 3command line socket tools
Key in Shared Memoryunix下编译fortran 的错误太多,看不到怎么办?
相关话题的讨论汇总
话题: int话题: write话题: interger话题: print话题: mistake