Python论坛  - 讨论区

标题:[python-chinese] 请教字符串格式化的一个奇怪问题.

2006年04月28日 星期五 15:51

shuai ant wyluanwu at gmail.com
Fri Apr 28 15:51:03 HKT 2006

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月28日 星期五 19:47

Robert Chen search.pythoner at gmail.com
Fri Apr 28 19:47:40 HKT 2006

可能跟浮点的实现有关,不奇怪啦,其实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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月28日 星期五 21:14

shuai ant wyluanwu at gmail.com
Fri Apr 28 21:14:55 HKT 2006

有没有什么解决的办法阿?我有个程序需要很精确的数字,郁闷。。。

在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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月28日 星期五 21:30

tocer tocer.deng at gmail.com
Fri Apr 28 21:30:37 HKT 2006

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月29日 星期六 10:48

helium helium.sun at gmail.com
Sat Apr 29 10:48:32 HKT 2006

#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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060429/d175edeb/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月29日 星期六 14:22

shuai ant wyluanwu at gmail.com
Sat Apr 29 14:22:59 HKT 2006

谢谢楼上两位的指点,基本搞定了...

在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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号