2006年04月28日 星期五 15:51
a=float(raw_input('输入:'))
c=a*55000
print c
print '%d' %c
显示结果为:
输入:2.05
112750.0
112749
结果为什么不一样呢?我试着输入了几个别的数,都没问题,好像只有2.05不对啊.比如:
输入:1.05
57750.0
57750
这是怎么回事啊?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060428/6c343612/attachment.htm
2006年04月28日 星期五 19:47
可能跟浮点的实现有关,不奇怪啦,其实C/C++中好像也有这样的现象,看看IEEE的浮点规范,或者《深入理解计算机系统》:) On 4/28/06, shuai ant <wyluanwu at gmail.com> wrote: > > a=float(raw_input('输入:')) > c=a*55000 > print c > print '%d' %c > > 显示结果为: > > 输入:2.05 > 112750.0 > 112749 > > 结果为什么不一样呢?我试着输入了几个别的数,都没问题,好像只有2.05不对啊.比如: > > 输入:1.05 > 57750.0 > 57750 > > 这是怎么回事啊? > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- Robert Python源码剖析――http://blog.donews.com/lemur/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060428/9e96ebd1/attachment-0001.htm
2006年04月28日 星期五 21:14
有没有什么解决的办法阿?我有个程序需要很精确的数字,郁闷。。。 在06-4-28,Robert Chen <search.pythoner at gmail.com> 写道: > > 可能跟浮点的实现有关,不奇怪啦,其实C/C++中好像也有这样的现象,看看IEEE的浮点规范,或者《深入理解计算机系统》:) > > On 4/28/06, shuai ant <wyluanwu at gmail.com> wrote: > > > a=float(raw_input('输入:')) > c=a*55000 > print c > print '%d' %c > > 显示结果为: > > 输入:2.05 > 112750.0 > 112749 > > 结果为什么不一样呢?我试着输入了几个别的数,都没问题,好像只有2.05不对啊.比如: > > 输入:1.05 > 57750.0 > 57750 > > 这是怎么回事啊? > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > -- > Robert > Python源码剖析――http://blog.donews.com/lemur/ > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060428/52eee224/attachment.html
2006年04月28日 星期五 21:30
a=float(raw_input('输入:'))
c=a*55000
print c
print '%f' %c # <----改成这样就可以,还可以设置精度,参看帮助
你原来的写法相当于取整了。
其实变量 c 的精度没有变化,只是打印出来是取整后的数。
你使用 c 再参与后面的计算,都不会有问题
shuai ant 写道:
> 有没有什么解决的办法阿?我有个程序需要很精确的数字,郁闷。。。
>
> 在06-4-28,*Robert Chen* <search.pythoner at gmail.com
> search.pythoner at gmail.com>> 写道:
>
> 可能跟浮点的实现有关,不奇怪啦,其实C/C++中好像也有这样的现象,看看
> IEEE的浮点规范,或者《深入理解计算机系统》:)
>
> On 4/28/06, *shuai ant* < wyluanwu at gmail.com
> wyluanwu at gmail.com>> wrote:
>
> a=float(raw_input('输入:'))
> c=a*55000
> print c
> print '%d' %c
>
> 显示结果为:
>
> 输入:2.05
> 112750.0
> 112749
>
> 结果为什么不一样呢?我试着输入了几个别的数,都没问题,好像只有2.05不对
> 啊.比如:
>
> 输入:1.05
> 57750.0
> 57750
>
> 这是怎么回事啊?
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> python-chinese at lists.python.cn>
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> Robert
> Python源码剖析――http://blog.donews.com/lemur/
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> python-chinese at lists.python.cn>
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年04月29日 星期六 10:48
#includeint main() { double d = 5.02; printf("%.17g\n", d); printf("%.12g\n", d); return 0; } C里一样有这个问题。要不你把小数都转成整数再运算。。。 在06-4-28,shuai ant <wyluanwu at gmail.com> 写道: > > 有没有什么解决的办法阿?我有个程序需要很精确的数字,郁闷。。。 > > 在06-4-28,Robert Chen <search.pythoner at gmail.com> 写道: > > > 可能跟浮点的实现有关,不奇怪啦,其实C/C++中好像也有这样的现象,看看IEEE的浮点规范,或者《深入理解计算机系统》:) > > > > On 4/28/06, shuai ant < wyluanwu at gmail.com> wrote: > > > > > a=float(raw_input('输入:')) > > c=a*55000 > > print c > > print '%d' %c > > > > 显示结果为: > > > > 输入:2.05 > > 112750.0 > > 112749 > > > > 结果为什么不一样呢?我试着输入了几个别的数,都没问题,好像只有2.05不对啊.比如: > > > > 输入:1.05 > > 57750.0 > > 57750 > > > > 这是怎么回事啊? > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > -- > > Robert > > Python源码剖析――http://blog.donews.com/lemur/ > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060429/d175edeb/attachment.html
2006年04月29日 星期六 14:22
谢谢楼上两位的指点,基本搞定了... 在06-4-29,helium <helium.sun at gmail.com> 写道: > > #include> > int main() > { > double d = 5.02; > printf("%.17g\n", d); > printf("%.12g\n", d); > return 0; > } > C里一样有这个问题。要不你把小数都转成整数再运算。。。 > > 在06-4-28,shuai ant <wyluanwu at gmail.com> 写道: > > > 有没有什么解决的办法阿?我有个程序需要很精确的数字,郁闷。。。 > > > > 在06-4-28,Robert Chen <search.pythoner at gmail.com > 写道: > > > > > 可能跟浮点的实现有关,不奇怪啦,其实C/C++中好像也有这样的现象,看看IEEE的浮点规范,或者《深入理解计算机系统》:) > > > > > > On 4/28/06, shuai ant < wyluanwu at gmail.com> wrote: > > > > > > > a=float(raw_input('输入:')) > > > c=a*55000 > > > print c > > > print '%d' %c > > > > > > 显示结果为: > > > > > > 输入:2.05 > > > 112750.0 > > > 112749 > > > > > > 结果为什么不一样呢?我试着输入了几个别的数,都没问题,好像只有2.05不对啊.比如: > > > > > > 输入:1.05 > > > 57750.0 > > > 57750 > > > > > > 这是怎么回事啊? > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request at lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > -- > > > Robert > > > Python源码剖析――http://blog.donews.com/lemur/ > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request at lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060429/189fa615/attachment.html
Zeuux © 2025
京ICP备05028076号