7

我意识到这个问题可能与处理器有关,但希望有人能指出我正确的方向。对于我的一生,我无法弄清楚如何将表示纳秒的 unsigned long long int 转换为 C 中表示秒的双精度(我使用 32 位大端 PowerPC 405 进行此特定测试,以及 GNU C99编译器)。

我试过了:

unsigned long long int nanoseconds = 1234567890LLU;
double nanoseconds_d = nanoseconds*1e-9;

还:

nanoseconds_d = ((double) nanoseconds)*1e-9;

对于这两种情况,我只得到 0。我在这里做错了什么?

编辑添加完整示例

#include <stdio.h>
#include <stdlib.h>

int
main( int argc, char *argv[] )
{
    unsigned long long int nanoseconds = 1234567890LLU;
    double nanoseconds_d = nanoseconds * 1e-9;
    printf("%g\n", nanoseconds_d);

    return 0;
}

制作文件

SRCS    = simple.c    
INCLUDE := -I$(PWD)            
CFLAGS  := -O0 -g3 -Wall -fmessage-length=0 -mhard-float -fsigned-char -D_REENTRANT
LIBS    := -lc

OBJS = $(SRCS:.c=.o)
PROG = $(SRCS:.c=).out

all: $(PROG)

$(PROG): $(OBJS)
    @echo "Linking object files with output."
    $(CC) -o $(PROG) $(OBJS) $(LIBS)
    @echo "Linking complete."

$(OBJS): $(SRCS)
    @echo "Starting compilation."
    $(CC) $(CFLAGS) $(INCLUDE) -c $<
    @echo "Compilation complete."   

clean::
    @$(RM) *.o *.out
4

2 回答 2

4

Works here when using %g to print

#include <stdlib.h>
#include <stdio.h>

int main(){
    unsigned long long int nanoseconds = 1234567890LLU;
    double nanoseconds_d = nanoseconds*1e-9;
    printf("%g\n", nanoseconds_d);

    nanoseconds_d = ((double) nanoseconds)*1e-9;
    printf("%g\n", nanoseconds_d);
}

outputs

1.23457
1.23457
于 2013-05-23T14:47:22.440 回答
0

事实证明,我使用的 cross gcc 版本有一个与软浮点与硬浮点有关的错误。因此,从 unsigned long long 到 double 的转换无法正常工作,从而导致了问题。

于 2013-05-23T18:52:32.790 回答