急急急急急----------会c的请进!!!!!!!!!!!!!

huanhuan2001 2002-02-04 07:28:06
我现在遇到了这样一个问题:
想把double中的整数部分放到unsigned long中,强制转换数不对。然后用modf取到double型整数部分,再转换还是不行。这是什么问题呢???请各位大虾帮忙!!!妹妹这里先谢过了!!!
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
huanhuan2001 2002-02-04
  • 打赏
  • 举报
回复
就是原来是unsigned long的,通过一定转换放到时间戳里,传到另一台机器后,再由时间戳转换到unsigned long里。
struct l_fixed_point{
unsigned long int_part;
unsigned long fraction;
};
在一端呢,作这样的变换,非这样做不可
void tick_to_stamp(unsigned long current_time,struct l_fixed_point *stamp)

{
double fractionf;
ldiv_t current;
//currentf=current_time;
//currentf/=100; /*conform 10ms to us*/
//fractionf=modef(currentf,&integerf);
current=ldiv(current_time,100); /*100/10hz,1000/100hz,10000/1000hz....*/
fractionf=current.rem*0.01;
fractionf=fractionf*4294967296.0;
stamp->fraction=fractionf;
stamp->int_part=current.quot;

};

在另一段为了还原,作如下变换
unsigned long stamp_to_tick(struct l_fixed_point stamp)
{
double intf,fracf;
double testcurrf,i,f;
static double currf;
unsigned long ticks_time;
intf=stamp.int_part;
fracf=stamp.fraction;
if(fracf)
{
fracf=fracf/4294967296.0;
}
else
{
fracf=0.0;
};
currf=intf+fracf;
currf=currf*100.0;
ticks_time=(unsigned long)currf;
//ticks_time++;
return(ticks_time);
};
请问有何不妥??????????
upig 2002-02-04
  • 打赏
  • 举报
回复
你用调试器看一看前后的值
d_selenite 2002-02-04
  • 打赏
  • 举报
回复
把你的程序贴上来啊,
huanhuan2001 2002-02-04
  • 打赏
  • 举报
回复
好象不是正负的问题,因为我的程序里double不可能是负数
intll 2002-02-04
  • 打赏
  • 举报
回复

double d = /* a double number */;

unsign long ul = ((unsign long) d > 0)? (unsign long ) d : (unsign long)(-d);
yanwuhuan 2002-02-04
  • 打赏
  • 举报
回复
怎么不行?
double dd=12341234.52341324;
unsigned long l;
l=dd;
或者 l=(unsigned long)dd;
都无不可。
1980xls 2002-02-04
  • 打赏
  • 举报
回复
它又不是十进制,是二进制呐!
hello_wyq 2002-02-04
  • 打赏
  • 举报
回复
是不是你的double是负的?

15,446

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧