Jump to content

(0.1 + 0.7) * 10 = ?


Veseloff
 Share

Recommended Posts

PHP

<?php 
echo (int)((0.1+0.7)*10);

Python

# -*- coding: utf-8 -*-
a = int((0.1 + 0.7) * 10);
print a

C

#include<stdio.h>
void main()
{
int n=(0.1+0.7)*10;
printf("%d \n", n);
}

Java себя показала правильнее C и не стала вообще компилиться.

Вопрос: как так?

Link to comment
Share on other sites

загуглили бы, проблеме сто лет в обед.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

http://ru.wikipedia.org/wiki/%D0%A7%D0%B8%D1%81%D0%BB%D0%B0_%D1%81_%D0%BF%D0%BB%D0%B0%D0%B2%D0%B0%D1%8E%D1%89%D0%B5%D0%B9_%D0%B7%D0%B0%D0%BF%D1%8F%D1%82%D0%BE%D0%B9

http://ru.wikipedia.org/wiki/%D0%A4%D0%B8%D0%BA%D1%81%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%BD%D0%B0%D1%8F_%D0%B7%D0%B0%D0%BF%D1%8F%D1%82%D0%B0%D1%8F

http://en.wikipedia.org/wiki/Floating_point

Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations.

http://www.php.net/manual/ru/language.types.float.php#warn.float-precision

"Java себя показала правильнее C и не стала вообще компилиться." - возможно "костыль" в компиляторе, который запрещает такое неявное (для человека) поведение

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy