C中int类型的sqrt() [英] sqrt() of int type in C

查看:420
本文介绍了C中int类型的sqrt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mac os x上用c语言编程。我在math.h中使用sqrt,函数如下:

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);
}

这有效,但为什么?为什么我没有得到警告?在sqrt的手册页上,它需要一个double作为参数,但我给它一个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?

谢谢

推荐答案

键入不会导致精度损失的转换可能不会引发警告。它们是隐含的。

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理由说它像指南


探索这个(隐式转换)问题的一个重要结果是理解高质量的编译器可能会很好地查找
这样可疑的代码并提供(可选的)诊断,并且
尽职尽责的教练可能会警告程序员隐式类型转换的
问题。

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类型的sqrt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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