This commit is contained in:
theo-l 2015-03-08 17:20:07 -03:00
commit 4b7d17b979
5 changed files with 60 additions and 104 deletions

View File

@ -1,44 +0,0 @@
translating by KayGuoWhu
Why does C++ promote an int to a float when a float cannot represent all int values?
---------
#Q:
Say I have the following:
```C
int i = 23;
float f = 3.14;
if (i == f) // do something
```
The i will be promoted to a float and the two float numbers will be compared, but can a float represent all int values? Why not promote both the int and the float to a double?
#A:
When `int` is promoted to `unsigned` in the integral promotions, negative values are also lost (which leads to such fun as `0u < -1` being true).
Like most mechanisms in C (that are inherited in C++), the usual arithmetic conversions should be understood in terms of hardware operations. The makers of C were very familiar with the assembly language of the machines with which they worked, and they wrote C to make immediate sense to themselves and people like themselves when writing things that would until then have been written in assembly (such as the UNIX kernel).
Now, processors, as a rule, do not have mixed-type instructions (add float to double, compare int to float, etc.) because it would be a huge waste of real estate on the wafer -- you'd have to implement as many times more opcodes as you want to support different types. That you only have instructions for "add int to int," "compare float to float", "multiply unsigned with unsigned" etc. makes the usual arithmetic conversions necessary in the first place -- they are a mapping of two types to the instruction family that makes most sense to use with them.
From the point of view of someone who's used to writing low-level machine code, if you have mixed types, the assembler instructions you're most likely to consider in the general case are those that require the least conversions. This is particularly the case with floating points, where conversions are runtime-expensive, and particularly back in the early 1970s, when C was developed, computers were slow, and when floating point calculations were done in software. This shows in the usual arithmetic conversions -- only one operand is ever converted (with the single exception of `long/unsigned int`, where the `long` may be converted to `unsigned long`, which does not require anything to be done on most machines. Perhaps not on any where the exception applies).
So, the usual arithmetic conversions are written to do what an assembly coder would do most of the time: you have two types that don't fit, convert one to the other so that it does. This is what you'd do in assembler code unless you had a specific reason to do otherwise, and to people who are used to writing assembler code and do have a specific reason to force a different conversion, explicitly requesting that conversion is natural. After all, you can simply write
```C
if((double) i < (double) f)
```
It is interesting to note in this context, by the way, that `unsigned` is higher in the hierarchy than `int`, so that comparing `int` with `unsigned` will end in an unsigned comparison (hence the `0u < -1` bit from the beginning). I suspect this to be an indicator that people in olden times considered `unsigned` less as a restriction on `int` than as an extension of its value range: We don't need the sign right now, so let's use the extra bit for a larger value range. You'd use it if you had reason to expect that an `int` would overflow -- a much bigger worry in a world of 16-bit ints.
----
via:[stackoverflow](http://stackoverflow.com/questions/28010565/why-does-c-promote-an-int-to-a-float-when-a-float-cannot-represent-all-int-val/28011249#28011249)
作者:[wintermute][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://stackoverflow.com/users/4301306/wintermute

View File

@ -1,43 +0,0 @@
translating by martin.
Nmap—Not Just for Evil!
================================================================================
If SSH is the Swiss Army knife of the system administration world, Nmap is a box of dynamite. It's really easy to misuse dynamite and blow your foot off, but it's also a very powerful tool that can do jobs that are impossible without it.
When most people think of Nmap, they think of scanning servers, looking for open ports to attack. Through the years, however, that same ability is incredibly useful when you're in charge of the server or computer in question. Whether you're trying to figure out what kind of server is using a specific IP address in your network or trying to lock down a new NAS device, scanning networks is incredibly useful.
Figure 1 shows a network scan of my QNAP NAS. The only thing I use the unit for is NFS and SMB file sharing, but as you can tell, it has a ton of ports wide open. Without Nmap, it would be difficult to figure out what the machine was running.
![](http://www.linuxjournal.com/files/linuxjournal.com/ufiles/imagecache/large-550px-centered/u1002061/11825nmapf1.jpg)
### Figure 1. Network Scan ###
Another incredibly useful way to use Nmap is to scan a network. You don't even have to have root access for that, and it's as simple as specifying the network block you want to scan. For example, typing:
nmap 192.168.1.0/24
will scan the entire range of 254 possible IP addresses on my local network and let me know which are pingable, along with which ports are open. If you've just plugged in a new piece of hardware, but don't know what IP address it grabbed via DHCP, Nmap is priceless. For example, the above command revealed this on my network:
Nmap scan report for TIVO-8480001903CCDDB.brainofshawn.com (192.168.1.220)
Host is up (0.0083s latency).
Not shown: 995 filtered ports
PORT STATE SERVICE
80/tcp open http
443/tcp open https
2190/tcp open tivoconnect
2191/tcp open tvbus
9080/tcp closed glrpc
This not only tells me the address of my new Tivo unit, but it also shows me what ports it has open. Thanks to its reliability, usability and borderline black hat abilities, Nmap gets this month's Editors' Choice award. It's not a new program, but if you're a Linux user, you should be using it!
--------------------------------------------------------------------------------
via: http://www.linuxjournal.com/content/nmap%E2%80%94not-just-evil
作者:[Shawn Powers][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://www.linuxjournal.com/users/shawn-powers

View File

@ -0,0 +1,43 @@
既然float不能表示所有的int那为什么在类型转换时C++将int转换成float
---------
#问题
代码如下:
```C
int i = 23;
float f = 3.14;
if (i == f) // 执行某段代码
```
编译器会将i转换成float类型然后比较这两个float的大小但是float能够表示所有的int吗为什么没有将int和float转换成double类型进行比较呢
#回答
在整型数的演变中,当`int`变成`unsigned`时,会丢掉负数部分(有趣的是,这样的话,`0u < -1`就是对的了
和C语言中的大部分机制在C++中得到继承一样就硬件操作而言常见的算术转换应该简明易懂。C语言的发明者精通他们所使用机器上的汇编语言他们编写的C语言对他们和像他们一样编写程序的人有直接的意义直到使用汇编语言编写诸如UNIX内核的程序时。
现如今一般来说处理器并不具有混合类型的指令系统如float和double相加、比较int和float诸如此类因为如果这样做造成芯片晶圆的巨大浪费——如果你想支持更多不同的类型你不得不实现更多的操作码。然而在实际中你只有实现"add int to int"、"compare float to float"和"multiply unsigned with unsigned"等功能的常见指令,这使得优先进行算术转换变得很有必要——它们是指令系统中两种类型的映射关系,它们中的大部分很有用处。
从习惯编写低级别机器代码的编程人员的角度来说如果有了混合类型那么在一般情况下最有可能使用的汇编指令就是那些只需要进行最少类型转换的指令。其中有一种特殊情况就是浮点数的转换特别是在20世纪70年代早期当时C语言正在被开发计算机运行速度很慢而浮点数的计算是通过软件完成的所以进行转换的成本很高。这拖慢了常用算术运算的转换开发——当时只有一种操作数实现了转换这个例外就是long到unsigned int的转换这种转换没有任何要求在大部分机器上都可以进行。当然并不是全部因为总有例外情况
所以,编写常用的算术转换是为了完成汇编程序员在大部分时间需要做的事情:即有两种不匹配的类型,将一种转换成另一种。这也就是汇编代码所做的事情,除非有特别原因需要进行其它类型转换。对于那些习惯编写汇编代码的人来说,除非是特殊需要,才会被迫去编写一种不同的类型转换。显然,这种情况下提出编写转换是很自然的事情。虽然,你可以简单地写成这样
```C
if((double) i < (double) f)
```
顺便提一下,在这个问题中有趣的是,`unsigned`的优先级高于`int`,所以把`int`和`unsigned`进行比较时最终进行的是unsigned类型的比较开头提到的`0u < -1`就是这个道理我猜测这可能是在早些时候计算机发展初期当时的人们认为`unsigned``int`在所表示的数值范围上受到的限制更小现在还不需要符号位所以可以使用额外的位来表示更大的数值范围如果你觉得`int`可能会溢出那么就使用unsigned好了在使用16位表示的ints时这个担心会更明显
----
via:[stackoverflow](http://stackoverflow.com/questions/28010565/why-does-c-promote-an-int-to-a-float-when-a-float-cannot-represent-all-int-val/28011249#28011249)
作者:[wintermute][a]
译者:[KayGuoWhu](https://github.com/KayGuoWhu)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://stackoverflow.com/users/4301306/wintermute

View File

@ -1,8 +1,8 @@
How To Fix: Failed to fetch cdrom apt-get update cannot be used to add new CD-ROMs
如何修复apt-get update无法添加新的CD-ROM
================================================================================
![](http://itsfoss.itsfoss.netdna-cdn.com/wp-content/uploads/2014/12/elementary_OS_Freya.jpg)
These days I am experimenting with Elementary OS Freya and during this, I encountered a very common updater error: **Failed to fetch cdrom Please use apt-cdrom to make this CD-ROM recognized by APT. apt-get update cannot be used to add new CD-ROMs**. The complete error looked like this after running the apt-get update command:
这些天我正在体验Elementary OS Freya在这期间我遇到了一个非常常见的更新错误**Failed to fetch cdrom Please use apt-cdrom to make this CD-ROM recognized by APT. apt-get update cannot be used to add new CD-ROMs**。完整的错误在运行apt-get update后看上去像这样
> W: Failed to fetch cdrom://elementary OS 0.3 _Freya_ Daily amd64 (20150208)/dists/trusty/main/binary-amd64/Packages Please use apt-cdrom to make this CD-ROM recognized by APT. apt-get update cannot be used to add new CD-ROMs
>
@ -10,33 +10,33 @@ These days I am experimenting with Elementary OS Freya and during this, I encoun
>
> E: Some index files failed to download. They have been ignored, or old ones used instead.
In this post, we shall see how to fix this error.
本篇中,我们会了解如何修复这个错误。
### Fix Failed to fetch cdrom apt-get update cannot be used to add new CD-ROMs error ###
### 修复apt-get update无法添加新的CD-ROM的错误 ###
The reason for this error is that cdrom has been included as one of the the sources here. And to fix this issue, we need to remove this from the list of software sources.
这个错误的原因是cdrom已经被包含在源之中。要修复这个问题我们需要将它从软件源中移除。
In Ubuntu, look for Software & Updates:
在Ubuntu中找到“软件与更新”
![](http://itsfoss.itsfoss.netdna-cdn.com/wp-content/uploads/2014/08/Software_Update_Ubuntu.jpeg)
In the first tab Ubuntu Software, look for the cdrom, if its checked, uncheck it.
在Ubuntu Software的第一个标签中找到cdrom如果它是勾选的那么取消勾选。
![](http://itsfoss.itsfoss.netdna-cdn.com/wp-content/uploads/2015/02/CDROM_Error_Update_Software_Sources.jpeg)
Close the Software Sources and run the update again. It should work fine now.
关闭软件源并再次运行更新。现在应该可以用了。
### Further troubleshoot: ###
### 进一步故障排除: ###
The method described above should have fixed this **apt-get update cannot be used to add new CD-ROMs** error. But this was not the case for me because the option of cdrom was already grayed out as I was using live session.
上面描述的方法已经修复了这个**apt-get update cannot be used to add new CD-ROMs**错误。但是这个方法对我无效因为cdrom的选项这时是灰色的因为我使用的live版本。
![](http://itsfoss.itsfoss.netdna-cdn.com/wp-content/uploads/2015/02/CDROM_Update_Error.png)
Now to fix our error, we shall take the command line route. Open a terminal and use the following line to see what is included in sources list:
现在要修复我们的问题了,我们是要采用命令行路线。打开终端并查看软件源中包含了哪些源:
cat /etc/apt/sources.list
The output for me was as following:
我的输出是下面这样:
deb cdrom:[elementary OS 0.3 _Freya_ Daily amd64 (20150208)]/ trusty main restricted
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
@ -46,17 +46,17 @@ The output for me was as following:
deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
Look at the first line in the above list. It includes cdrom. We need to comment out this line by adding # in front of it to make it look like this:
在上面的第一行中。它包含了cdrom。我们需要用#’来注释掉这行:
#deb cdrom:[elementary OS 0.3 _Freya_ Daily amd64 (20150208)]/ trusty main restricted
To do that use the command below:
要用下面的命令来:
sudo gedit /etc/apt/sources.list
Once you have edited the sources.list, run the apt-get update once again. The error apt-get update cannot be used to add new CD-ROMs should have been fixed. If you are facing any other update issue, do look at this article which is a collection of most [common Ubuntu update error fixes][1].
在你编辑完软件源后再次运行apt-get update。“apt-get update cannot be used to add new CD-ROMs”这个错误应该已经修复了。如果你还遇到其他的问题看一下这篇收集了大部分[Ubuntu常见更新错误修复][1]的文章。
I hope you found this tutorial helpful. If you have any questions or suggestions, feel free to drop a comment.
我希望这篇教程对你有用。如果你还有其他的问题和建议,请在下面留言。
--------------------------------------------------------------------------------
@ -69,4 +69,4 @@ via: http://itsfoss.com/fix-failed-fetch-cdrom-aptget-update-add-cdroms/
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://itsfoss.com/author/abhishek/
[1]:http://itsfoss.com/fix-update-errors-ubuntu-1404/
[1]:http://itsfoss.com/fix-update-errors-ubuntu-1404/