簡體   English   中英

Double 函數以整數形式返回

[英]Double function returning as an integer

我想知道為什么我的 double 函數返回的是整數而不是小數。 我給我的 ic4 賦值為 0.01 以進入函數並期望返回 0.384615,但我得到的返回值為 1。

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>

using namespace std;

double vt = 0.026;
double ic4;

double gm7(double IC7);

int main ()
{
    while(true)
    {
        printf("ao (in dB): ");
        cin >> ao;

        if (ao >= 80)
        {
            printf("IC7 (in Amps): ");
            cin >> ic7;
            cout << "IC7: " << ic7 << endl;
            gm7(ic7);
            cout <<"gm7: " << gm7 << endl;
        }
    
        else
        {
            printf("Choose another ao!\n");
        }
    }
}

double gm7 (double IC7)
{
    return IC7 / vt;
}

gm7是一個函數。 您正在向字符流中插入一個函數。

字符流沒有函數的插入運算符重載。 但是,它們確實有bool的重載,函數指針隱式轉換為bool ,函數隱式轉換為函數指針。 由於函數指針不為空,它會轉換為true true打印為 1。

PS 示例程序格式錯誤,因為它使用了未聲明的名稱。

只是為了澄清評論

你應該做這個

int main ()
{
    while(true)
    {
        printf("ao (in dB): ");
        cin >> ao;

        if (ao >= 80)
        {
            printf("IC7 (in Amps): ");
            cin >> ic7;
            cout << "IC7: " << ic7 << endl;
            double gm7res = gm7(ic7); <======
            cout <<"gm7: " << gm7res << endl; <<======
        }
    
        else
        {
            printf("Choose another ao!\n");
        }
    }
}

請注意,您還需要聲明 a0

暫無
暫無

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

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