Active Members MrGrj Posted November 19, 2014 Active Members Report Posted November 19, 2014 (edited) Fara a rula urmatorul cod si fara a cauta pe internet, ce va afisa aceasta linie de cod ?Argumentati raspunsul dat. <?php echo (int) ( (0.1 + 0.7) * 10); ?>Solver:- Ganav.Raspuns complet (adaugat la ce a zis Ganav): "IEEE 754 cannot be used for 0.1 as well as 0.7 ... so the float point addition becomes 0.799(9...) x 10 = 7.99(9...) and int typecast truncates the digits after decimal => 7 "Raspuns dat:Din cate stiu in PHP (int) rotunjeste in jos. Adica 0.1 nu este 0.1 si 0.1 - e unde e este un numar subunitar care este folosit pentru a reda precizia. Deci 0.1 este < 0.1, la fel si cu 0.7. De aici rezulta ca 0.1 + 0.7 < 0.8. Analog, 10 * ( 0.1 + 0.7) < 10 * 0.8. (int) rotunjind in jos ajunge la 7(valoarea intreaga imediat mai mica ca si 8). Edited November 19, 2014 by MrGrj Quote
InjectedAndrei Posted November 19, 2014 Report Posted November 19, 2014 (edited) 8 , la prima vedereDar. Numerele sunt intregi. Deci 0.1 = 1 / 0.7 = 1 --> (1+1)*10 = 20. Edited November 19, 2014 by InjectedAndrei Quote
eusimplu Posted November 19, 2014 Report Posted November 19, 2014 Nu mai faceti misto de float-urile php. Quote
Active Members MrGrj Posted November 19, 2014 Author Active Members Report Posted November 19, 2014 8 , la prima vedereDar. Numerele sunt intregi. Deci 0.1 = 1 / 0.7 = 1 --> (1+1)*10 = 20.Wow.Perceptia mea privind logica si informatica s-a schimbat total.Total gresit. Mai gandeste-te Quote
leaklife Posted November 19, 2014 Report Posted November 19, 2014 Parte intreaga din (0.1+0.8)*10 e tot 8 pentru ca il face intreg dupa ce se calculeaza. Quote
Active Members MrGrj Posted November 19, 2014 Author Active Members Report Posted November 19, 2014 Nu e 8 raspunsul. Quote
InjectedAndrei Posted November 19, 2014 Report Posted November 19, 2014 M-am documentat putin despre asta si am aflat raspunsul si explicatia. Foarte funny task-ul Quote
leaklife Posted November 19, 2014 Report Posted November 19, 2014 Nu e 8 raspunsul.M-am prins... E zero, pentru ca parte intreaga din 0.1 si 0.8 e zero, evident. Si 10*0=0. Quote
Ganav Posted November 19, 2014 Report Posted November 19, 2014 0.7? Din cate stiu in PHP (int) rotunjeste in jos. Adica 0.1 nu este 0.1 si 0.1 - e unde e este un numar subunitar care este folosit pentru a reda precizia. Deci 0.1 este < 0.1, la fel si cu 0.7. De aici rezulta ca 0.1 + 0.7 < 0.8. Analog, 10 * ( 0.1 + 0.7) < 10 * 0.8. (int) rotunjind in jos ajunge la 7(valoarea intreaga imediat mai mica ca si 8). 1 Quote
Active Members MrGrj Posted November 19, 2014 Author Active Members Report Posted November 19, 2014 (edited) @Ganav - corect //challenge closed. Edited November 19, 2014 by MrGrj Quote
Hertz Posted November 19, 2014 Report Posted November 19, 2014 Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118.... Mai mult detalii in chenarul roz de aici: PHP: Floating point numbers - Manual Quote