簡體   English   中英

如何結合變量來表示C中的科學計數法?

[英]How to combine variables to represent scientific notation in C?

如果我有

a = 3;
b = 5;

我該怎么做

雙重結果= 3e5,但僅使用變量?

我知道aeb顯然行不通。

嘗試:

double result = a * pow(10.0,(double)b);

或者,使用GNU擴展:

double result = a * exp10((double)b);

無論哪種情況,都#include math.h並鏈接到數學庫(例如-lm )。 這可能比將字符串拼湊在一起並轉換為雙精度要有效得多。

使用stdlib.hsprintf定義的atof函數:

float a = 3;
int b = 5;

char tmp[10];
sprintf(tmp, "%fe%d", a, b);

double x = atof(tmp);
printf("x = %fe%d = %f\n", a, b, x);

輸出: http : //ideone.com/NdDcNB

x = 3.000000e5 = 300000.000000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM