在C开方回吐INT [英] sqrt taking int in c

查看:119
本文介绍了在C开方回吐INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程在Mac OS X上C语言。我使用的开方,从math.h中,功能是这样的:

I am programming in the c language on mac os x. I am using sqrt, from math.h, function like this:

int start = Data -> start_number;
double localSum;

for (start; start <= end; start++) {
    localSum += sqrt(start);
}

这工作,但为什么呢?而为什么我没有得到警告?对开方手册页,它采用双象参数,但我给它一个int - ?它如何能工作

This works, but why? and why am I getting no warning? On the man page for sqrt, it takes a double as parameter, but I give it an int - how can it work?

感谢

推荐答案

类型转换其中的不会引起在precision亏损可能不会抛出警告。他们是隐式转换。

Type conversions which do not cause a loss in precision might not throw warnings. They are cast implicitly.

int --> double //no loss in precision (e.g 3 became 3.00)
double --> int //loss in precision (e.g. 3.01222 became 3)

什么触发警告,什么不就是在很大程度上取决于编译器,并提供给它的标志,但是,大多数编译器(至少我使用过的)的不考虑隐类型转换危险足以保证一个警告,因为它是在语言规范的特点。

What triggers a warning and what doesn't is depends largely upon the compiler and the flags supplied to it, however, most compilers (atleast the ones I've used) don't consider implicit type-conversions dangerous enough to warrant a warning, as it is a feature in the language specification.

C99原理指出它像一个指引

C99 Rationale states it like a guideline

一个探索这个(隐式转换)的重要成果的问题是认识到高品质的编译器可能会做得很好看
  此类可疑code,并提供(可选)诊断,以及
  兢兢业业的导师可能会做的很好,警告的程序员
  隐式类型转换的问题。

One of the important outcomes of exploring this (implicit casting) problem is the understanding that high-quality compilers might do well to look for such questionable code and offer (optional) diagnostics, and that conscientious instructors might do well to warn programmers of the problems of implicit type conversions.

C99依据(2003年4月):第45

C99 Rationale (Apr 2003) : Page 45

这篇关于在C开方回吐INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆