Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    465

    2 semplici cicli for: problemi di arrotondamento con float?

    Provate ad eseguire questo semplice codice

    codice:
    <?php
    for ($i=0;$i<1.50;$i=$i+0.01)
    { $a[$i*100]=$a[$i*100]+$i*100;
    }
    for ($i=0;$i<150;$i++)
    { echo ("i è " . $i . " a è " . $a[$i] . "
    ");
    }
    ?>
    il risultato non è diverso da quanto ci si potesse aspettare?

  2. #2
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    meglio non giocare con i decimali negli indici dei vettori che devono essere interi............

  3. #3
    Dal manuale ufficiale:

    codice:
    array(  key =>  value
         , ...
         )
    // key may only be an integer or string
    // value may be any value of any type
    A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    465
    Ok deve essere un intero.

    Ma se io moltiplico per 100 un numero con 2 cifre dopo la virgola dovrebbe uscire sempre un intero...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.