简体   繁体   English

Double 函数以整数形式返回

[英]Double function returning as an integer

I would like to know why does my double function returns as an integer instead of a decimal.我想知道为什么我的 double 函数返回的是整数而不是小数。 I gave a value of 0.01 to my ic4 to go into the function and expect a return of 0.384615 but instead i get a return of 1.我给我的 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 is a function. gm7是一个函数。 You are inserting a function into the character stream.您正在向字符流中插入一个函数。

Character streams do no have an insertion operator overloads for functions.字符流没有函数的插入运算符重载。 However, they do have an overload for bool and function pointers implicitly convert to bool and function implicitly converts to a function pointer.但是,它们确实有bool的重载,函数指针隐式转换为bool ,函数隐式转换为函数指针。 Since the function pointer is not null, it converts to true .由于函数指针不为空,它会转换为true true is printed as 1. true打印为 1。

PS The example program is ill-formed because it uses undeclared names. PS 示例程序格式错误,因为它使用了未声明的名称。

just to clarify the comments只是为了澄清评论

you should do this你应该做这个

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");
        }
    }
}

note you also need to declare a0请注意,您还需要声明 a0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM