modf(),fmod()如何为小数部分返回1.0,为什么? [英] how can modf(), fmod() return 1.0 for the fractional part, why?

查看:78
本文介绍了modf(),fmod()如何为小数部分返回1.0,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到一个浮点的小数部分

数。我已经尝试了fmod()和modf(),虽然两者都有效,但它们

偶尔也会返回1.0的小数部分

而不是0.0 。这是在使用xcode的mac上。这应该不会发生吗?b / b
发生了吗?


谢谢

Bob

解决方案

在文章< e9 ********************************** @ q9g2000hsb .g ooglegroups.com>,

< bs ******* @ gmail.comwrote:


>我正试图得到一个浮点数的小数部分。我已经尝试了fmod()和modf(),虽然两者都有效,但它们偶尔也会返回1.0代表数字的小数部分而不是0.0。这是在使用xcode的mac上。这不应该发生吗?



你怎么知道他们要回1.0?你打印出去了吗?

请记住,如果你没有使用足够的数字,像0.9999999999999这样的值可能会打印为1.0


< br $>
- 理查德

-

请记得提及我/你留下的录音带。


9月29日,4:09 * pm,富... @ cogsci.ed.ac.uk(Richard Tobin)写道:


In文章< e956e321-7437-4682-a2a3-f0163bfa1 ... @ q9g2000hsb.googlegroups.com>,


*< bsabis ... @ gmail.comwrote:


*我正在尝试获得浮点的小数部分

数。 *我已经尝试了fmod()和modf(),虽然两者都有效,但它们

偶尔也会返回1.0的小数部分

而不是0.0。 *这是在使用xcode的mac上。 *这应该永远不会发生
应该发生吗?



你怎么知道他们要回1.0? *你打印出来吗?

请记住,如果你没有使用足够的数字,像0.9999999999999这样的值可能会被打印为1.0




- 理查德

-

请记得提及我/你留下的录音带。



hmm。好吧也许这种情况正在发生。我正在使用printf(%f

\ n,fmod(val));


bsabis ... @ gmail.com写道:


我正在尝试获得浮点的小数部分

数。我已经尝试了fmod()和modf(),虽然两者都有效,但它们

偶尔也会返回1.0的小数部分

而不是0.0 。这是在使用xcode的mac上。这应该永远不会发生b $ b b应该发生吗?



我最好的猜测是你得到的数字只是略微小于1.0的b $ b,当你打印出来的时候,它会四舍五入为1.0。


您是否检查过与1.0相等的返回值?如果你只是将它打印出去,你是否打印出足够重要的

数字来区分nextafter(1.0,0.0)和1.0?如果

你使用下面的printf()语句怎么办?


#include< float.h>

#include< stdio.h>


printf("%。* g\ n",DBL_DIG + 1,fmod(x,1.0))


Hi, I''m trying to get the fractional part of a floating point
number. I''ve tried fmod() and modf(), and while both do work, they
also occasionally return 1.0 for the fractional part of the number
instead of 0.0. This is on a mac using xcode. This should not ever
happen should it?

Thanks
Bob

解决方案

In article <e9**********************************@q9g2000hsb.g ooglegroups.com>,
<bs*******@gmail.comwrote:

>Hi, I''m trying to get the fractional part of a floating point
number. I''ve tried fmod() and modf(), and while both do work, they
also occasionally return 1.0 for the fractional part of the number
instead of 0.0. This is on a mac using xcode. This should not ever
happen should it?

How do you know that they''re returning 1.0? Are you printing it out?
Bear in mind that a value like 0.9999999999999 may get printed as 1.0
if you don''t use enough digits.

-- Richard
--
Please remember to mention me / in tapes you leave behind.


On Sep 29, 4:09*pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:

In article <e956e321-7437-4682-a2a3-f0163bfa1...@q9g2000hsb.googlegroups.com>,

*<bsabis...@gmail.comwrote:

Hi, *I''m trying to get the fractional part of a floating point
number. *I''ve tried fmod() and modf(), and while both do work, they
also occasionally return 1.0 for the fractional part of the number
instead of 0.0. *This is on a mac using xcode. *This should not ever
happen should it?


How do you know that they''re returning 1.0? *Are you printing it out?
Bear in mind that a value like 0.9999999999999 may get printed as 1.0
if you don''t use enough digits.

-- Richard
--
Please remember to mention me / in tapes you leave behind.

hmm. well maybe that is happening. I''m using printf("%f
\n",fmod(val));


bsabis...@gmail.com wrote:

Hi, I''m trying to get the fractional part of a floating point
number. I''ve tried fmod() and modf(), and while both do work, they
also occasionally return 1.0 for the fractional part of the number
instead of 0.0. This is on a mac using xcode. This should not ever
happen should it?


My best guess is that you''re getting a number that is only slightly
smaller than 1.0, and when you print it out, it gets rounded to 1.0.

Have you checked the return value for equality with 1.0? If you are
just printing it out, are you printing it with enough significant
digits to distinguish nextafter(1.0, 0.0) from 1.0? What do you get if
you use the following printf() statement?

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

printf("%.*g\n", DBL_DIG+1, fmod(x, 1.0))


这篇关于modf(),fmod()如何为小数部分返回1.0,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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