[Rio-pm] Chaves de hash
thiagoglauco em ticursos.net
thiagoglauco em ticursos.net
Quinta Novembro 29 00:47:34 PST 2012
Existe um problema aqui, diferente da comparação.
A comparação de reais é problemática devido a estrutura interna do
ponto flutuante. NÃO SE COMPARA REAIS POR MAIOR OU MENOR.Isso não é do
Perl, mas das regras de aproximação usada pelos processadores.
Rounding rules
The standard defines five rounding rules. The first two round to a
nearest value; the others are called directed roundings:
Roundings to nearest
Round to nearest, ties to even – rounds to the nearest value; if
the number falls midway it is rounded to the nearest value with an even
(zero) least significant bit, which occurs 50% of the time; this is the
default for binary floating-point and the recommended default for
decimal.
Round to nearest, ties away from zero – rounds to the nearest
value; if the number falls midway it is rounded to the nearest value
above (for positive numbers) or below (for negative numbers); this is
intended as an option for decimal floating point.
Directed roundings
Round toward 0 – directed rounding towards zero (also known as
truncation).
Round toward +∞ – directed rounding towards positive infinity (also
known as rounding up or ceiling).
Round toward −∞ – directed rounding towards negative infinity (also
known as rounding down or floor).
Se a comparação for inevitável, converta o seu número para string e
compare ou determine um nível de precisão aceitável e faça bit a bit.
Em 2012-11-28 21:42, Aureliano Guedes escreveu:
> Ola Monges.
>
> Estou com um problema simples mas que não acho a solução.
>
> Eu tenho um hash onde as chaves são valores numericos reais (a
> maioria negativo e quase nenhum inteiro).
> Estou limitando esses valores por um maximo e um minimo.
>
> foreach my $keys (keys %d) {
> if ($keys <= $min and $keys >= $max) {
> print "$d{$keys}";
> }
> }
>
> Problema que não da certo.
> Ha algo de errado aqui??
>
> _______________________________________________
> Rio-pm mailing list
> Rio-pm em pm.org
> http://mail.pm.org/mailman/listinfo/rio-pm
Mais detalhes sobre a lista de discussão Rio-pm