将序列与浮点数相乘 [英] Multiplying sequences with floats

查看:121
本文介绍了将序列与浮点数相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,如果你写3 *''*'',你会得到''***'',但是如果你写了

3.0 *''*'',你会得到错误(不能将序列乘以非int)。


我想知道这是否应该被允许,即

序列的乘法与一个浮子。可能有一个隐含的类型转换为
(即舍入),或者只有具有

小数部分的浮点数才会出现上述错误。用法示例:您想要转换百分比值

(到0到4星的数量。您可以使用表达式


百分比/ 20 *''*''


但是,如果百分比是浮点数,则会失败。即使这样也失败了:


百分比// 20 * ''*''


所以你必须写


int(百分比// 20)*''*''


在这种情况下你也可以写一下


int(百分比/ 20)*''*''


再次。好吧,这可能不是什么大不了的事情,但它不知何故让我感到很奇怪

,你不能简单地写出百分比// 20 *''*''。


- Christoph

Currently, if you write 3*''*'', you will get ''***'', but if you write
3.0*''*'', you will get an error (can''t multiply sequence by non-int).

I was wondering whether this should be allowed, i.e. multiplication of a
sequence with a float. There could be either an implicit typecast to int
(i.e. rounding), or the above error could occur only for floats with a
fractional part. Usage example: You want to convert a percentage value
(to a number of 0 to 4 stars. You could do this with the expression

percentage/20*''*''

However, this fails if percentage is a float. And even this fails:

percentage//20*''*''

So you have to write

int(percentage//20)*''*''

in which case you may as well write

int(percentage/20)*''*''

again. Ok, it''s probably not a big deal but it somehow stroke me as odd
that you can''t simply write percentage//20*''*''.

-- Christoph

推荐答案

2006年3月24日星期五00:35:44 +0100,

Christoph Zwerschke< ci ** @ online.de>写道:
On Fri, 24 Mar 2006 00:35:44 +0100,
Christoph Zwerschke <ci**@online.de> wrote:
目前,如果你写3 *''*'',你会得到''***'',但如果你写的是
3.0 * *,你会得到一个错误(can''t乘以非INT序列)。
我不知道这是否应该被允许,即有浮动的序列。可能有一个隐含的类型转换为
int(即舍入)......
Currently, if you write 3*''*'', you will get ''***'', but if you write
3.0*''*'', you will get an error (can''t multiply sequence by non-int). I was wondering whether this should be allowed, i.e. multiplication of
a sequence with a float. There could be either an implicit typecast to
int (i.e. rounding) ...




显式优于隐式。


至少在未来的某个版本的python中,math.sqrt(4.0)

返回一个整数。 ;-)


问候,

Dan


-

Dan Sommers

< http://www.tombstonezero.net/dan/>

我希望人们按字母顺序死亡。 - 我的妻子,系谱学家



Explicit is better than implicit.

At least until some future version of python in which math.sqrt( 4.0 )
returns an integer. ;-)

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist


Dan Sommers写道:
Dan Sommers wrote:
Christoph Zwerschke写道:
Christoph Zwerschke wrote:
我想知道是否这应该是允许的,即序列与浮点的乘法。可能有一个隐含的类型转换为int(即舍入)......
明确比隐含更好。
I was wondering whether this should be allowed, i.e. multiplication of
a sequence with a float. There could be either an implicit typecast to
int (i.e. rounding) ...
Explicit is better than implicit.




我已经知道使用单词隐含会激起那个答案...

无论如何,这只是一个论证,只针对类型转换的变种

一个带有小数部分的浮点数。但是如果有一个小数部分,那么

会引发错误的另一个变种呢,但如果浮点数是
实际上是一个完全整数,就像4.0 /的结果一样/ 2。


顺便说一句,一个序列与一个int的乘法会产生更多的隐含的东西:如果是这个数字则认为是零否定。

至少在未来的某个版本的python中,math.sqrt(4.0)
返回一个整数。 ; - )



I already knew using the word "implicit" would provoke that answer...
Anyway this would be an argument only against the variant of typecasting
a float with a fractional part. But what about the other variant which
raises an error if there is a fractional part, but works if the float is
actually an exact integer, like the result of 4.0//2.

BTW, the multiplication of a sequence with an int does an even more
implicit thing: The number is considered to be zero if it is negative.
At least until some future version of python in which math.sqrt( 4.0 )
returns an integer. ;-)




在那个版本中,4.0 // 2也会返回一个整数......但那不是我b / b
记住。


- Christoph



In that version, 4.0//2 would also return an integer... But that is not
something I had in mind.

-- Christoph


" Christoph Zwerschke" < CI ** @ online.de>在消息中写道

news:dv ********** @ online.de ...
"Christoph Zwerschke" <ci**@online.de> wrote in message
news:dv**********@online.de...
无论如何这只是一个参数反对使用小数部分进行类型转换的变体。但是如果有一个小数部分会导致错误的另一个变体呢,但如果浮点数实际上是一个精确的整数,就像4.0 // 2的结果一样。
Anyway this would be an argument only against the variant of typecasting a
float with a fractional part. But what about the other variant which
raises an error if there is a fractional part, but works if the float is
actually an exact integer, like the result of 4.0//2.




我认为这是一个非常糟糕的主意,因为结果

可能恰好是一个实现上的完全整数而不是另一个。

在这种情况下,实现X可能失败的事实很可能无法通过对实现Y的任何数量的测试来检测。这样的

不可测试的错误是如此令人讨厌,以至于鼓励程序员给他们一个广泛的停泊点更好的语言




I think that''s a really bad idea, because of the possibility that the result
might happen to be an exact integer on one implementation but not another.
In such situations, the fact that it might fail on implementation X may well
be impossible to detect by any amount of testing on implementation Y. Such
untestable errors are such a nuisance that it would better for the language
to encourage programmers to give them a wide berth.


这篇关于将序列与浮点数相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆