从函数返回浮点值 [英] returning a floating point value from a function

查看:193
本文介绍了从函数返回浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得返回浮点数的.xxx部分的函数?


Ivan。

How can I get a function to return the .xxx part of a floating point number?

Ivan.

推荐答案

Ivan Leo Puoti写道:
Ivan Leo Puoti wrote:
如何获得一个函数来返回.xxx部分的浮点数?
How can I get a function to return the .xxx part of a floating point
number?



减去积分部分并返回差值。


double frac(double d){

int i = d;

返回d - i;

}


-

Joe Wright mailto: jo********@comcast.net

一切都应该尽可能简单,但不能简单。

--- Albert爱因斯坦---


Subtract the integral part and return the difference.

double frac(double d) {
int i = d;
return d - i;
}

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---




" Joe Wright" <乔******** @ comcast.net>在消息中写道

"Joe Wright" <jo********@comcast.net> wrote in message
Ivan Leo Puoti写道:
Ivan Leo Puoti wrote:
我怎么能得到一个函数来返回浮动
点数的.xxx部分?
How can I get a function to return the .xxx part of a floating
point number?


减去积分部分并返回差异。

双重压裂(双d){
int i = d;
返回d - i;
}


Subtract the integral part and return the difference.

double frac(double d) {
int i = d;
return d - i;
}



使用floor()函数并考虑如何处理负数

数字。

如果参数

的大小超出整数范围,则转换为int会产生错误的结果。


Use the floor() function and also consider how you want to handle negative
numbers.
Casting to int will give wrong results if the size of the parameter
overflows the range of an integer .


Joe Wright写道:
Joe Wright wrote:
Ivan Leo Puoti写道:
Ivan Leo Puoti wrote:
如何获得一个函数来返回.xxx部分的浮点数?
How can I get a function to return the .xxx part of a floating point
number?


双重压裂(双d){
int i = d;
返回d - i;
}


Subtract the integral part and return the difference.

double frac(double d) {
int i = d;
return d - i;
}




对不起,但没有:


#include< limits.h>

#include< stdio.h>

#include< math.h>

#include< float.h>


/ * Joe Wright''贡献* /

双倍压裂(双倍)

{

int i = d;

返回d - i;

}

int main(无效)

{

double fpnum;

double fpart;

double ipart;

printf(" Ivan Leo Puoti问:\ n"

" \我怎样才能获得一个函数来返回\ n

" .xxx浮点数的一部分?\" \ n"

" Joe Wright声称:\ n"

" \减去整数部分并返回\ n

差异。\" \ n" 让我们测试他的代码。\ n

" *警告*此测试的结果将因\\ n而不是b / b而变化。执行。\ nn \ n");

fpnum = INT_MAX;

printf(" INT_MAX =%d,fpnum =%。* g\\\
" ;,INT_MAX,DBL_DIG,fpnum);

fpnum + = 3.37;

printf(" fpnum modified to%。* g\ n",DBL_DIG,fpnum) ;

fpart = frac(fpnum);

printf(" Joe Wright'的代码声称小数部分是%g \ n"

(并告诉我们没有任何组成部分。)\ n \ n",fpart);

fpart = modf(fpnum,& ipart);

printf(" modf将小数部分返回为%g \ n"

"以及整数部分为%。* g\ n,fpart,DBL_DIG,ipart) ;

返回0;

}

Ivan Leo Puoti问:

"我怎样才能获得一个功能返回

.xxx浮点数的一部分?

Joe Wright声称:

减去整数部分并返回

差价。

让测试他的代码。

*警告*此测试的结果将随实施而变化




INT_MAX = 2147483647,fpnum = 2147483647

fpnum修改为2147483650.37

Joe Wright'的代码声称小数部分是4.29497e + 09

(和告诉我们什么是不可或缺的部分。)


modf将小数部分返回为0.37

和积分部分为2147483650



Sorry, but no:

#include <limits.h>
#include <stdio.h>
#include <math.h>
#include <float.h>

/* Joe Wright''s contribution */
double frac(double d)
{
int i = d;
return d - i;
}
int main(void)
{
double fpnum;
double fpart;
double ipart;
printf("Ivan Leo Puoti asked:\n"
" \"How can I get a function to return the\n"
" .xxx part of a floating point number?\"\n"
"Joe Wright claimed:\n"
" \"Subtract the integral part and return\n"
" the difference.\"\n" "Let''s test his code.\n"
"*Warning* The results of this test will vary with\n"
" the implementation.\n\n");
fpnum = INT_MAX;
printf("INT_MAX = %d, fpnum = %.*g\n", INT_MAX, DBL_DIG, fpnum);
fpnum += 3.37;
printf("fpnum modified to %.*g\n", DBL_DIG, fpnum);
fpart = frac(fpnum);
printf("Joe Wright''s code claims the fractional part is %g\n"
" (and tells us nothing of the integral part.)\n\n", fpart);
fpart = modf(fpnum, &ipart);
printf("modf returns the fractional part as %g\n"
" and the integral part as %.*g\n", fpart, DBL_DIG, ipart);
return 0;
}
Ivan Leo Puoti asked:
"How can I get a function to return the
.xxx part of a floating point number?"
Joe Wright claimed:
"Subtract the integral part and return
the difference."
Let''s test his code.
*Warning* The results of this test will vary with
the implementation.

INT_MAX = 2147483647, fpnum = 2147483647
fpnum modified to 2147483650.37
Joe Wright''s code claims the fractional part is 4.29497e+09
(and tells us nothing of the integral part.)

modf returns the fractional part as 0.37
and the integral part as 2147483650


这篇关于从函数返回浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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