查看: 1132|回复: 6
收起左侧

[其他] 大佬们看这个c语言有没有好办法增大它的指数范围

[复制链接]
南山不会落梅花
发表于 2022-11-25 07:35:41 | 显示全部楼层 |阅读模式
本帖最后由 南山不会落梅花 于 2022-11-25 07:42 编辑

//幂的计算
#include<stdio.h>
int main(void)
{
        printf("请输入底数:\n");
        float num;
        scanf_s("%f", &num);
        getchar();
        printf("请输入指数:(2≤指数≤4)\n");
        int exponent;
        scanf_s("%d", &exponent);
        getchar();

        if(exponent ==2)
        {
                float result = num * num;
                printf("结果是:%f", result);
        }
                if(exponent ==3)
        {
                float result = num * num * num;
                printf("结果是:%f", result);
        }
                if(exponent ==4)
        {
                float result = num * num * num * num;
                printf("结果是:%f", result);
        }

        getchar();
        return 0;
}

大佬们看这个c语言有没有好办法增大它的指数范围

YorkWaugh
发表于 2022-11-25 20:24:39 | 显示全部楼层
  1. #include <math.h>
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     double a,x;
  6.     printf("请输入底数:\n");
  7.     scanf("%lf", &a);
  8.     printf("请输入指数:\n");
  9.     scanf("%lf", &x);
  10.     printf("结果是:%lf\n", pow(a, x));
  11.     return 0;
  12. }
复制代码

不理解你的要求,这样可以吗
带刀侍卫
发表于 2022-11-25 21:18:34 | 显示全部楼层
YorkWaugh 发表于 2022-11-25 20:24
不理解你的要求,这样可以吗

楼主想算底数=250,指数=250,结果是多少
os52
发表于 2022-11-25 21:27:09 | 显示全部楼层
List of arbitrary-precision arithmetic software

Package-library nameNumber typeLanguageLicense
Boost Multiprecision LibraryIntegers, rationals, floats, and complexC++ and backends using GMP/MPFRBoost
TTMathIntegers, floatsC++BSD
LibBFIntegers, floatsCMIT
BeeNumIntegers, rationalsC++MIT
longer-intIntegersCGPL
GNU Multi-Precision Library (and MPFR)Integers, rationals, and floatsC and C++ with bindingsLGPL
CLNIntegers, rationals, floats, and complexC++GPL
ARPRECIntegers, floats, and complexC++BSD-type
MAPM, MAPMIntegers, decimal and complex floatsC (bindings for C++)Freeware
MPIR (mathematics software)Integers, rationals, and floatsC and C++ with bindingsLGPL
COREIntegers, rationals, and floatsC++Freeware
LEDAIntegers, rationals, and floatsC++Freeware
CGALIntegers, rationals, and floatsC++LGPL
GeometricToolsIntegers and rationalsC++Boost
LibTomMathIntegersCPublic Domain or WTFPL (dual-licensed)
libgcryptIntegersCLGPL
OpenSSLIntegersCApache License v2
ArbitraireFloatsCMIT License
mbed TLSIntegersCApache License v2 and GPL
JScienceIntegers, rationals, and floatsJavaBSD-type
JASIntegers, rationals, and complex numbersJavaLGPL
JLinAlgDecimals, rationals, and complex numbersJavaLGPL
ApfloatIntegers, rationals, floats, and complex numbersJava, C++LGPL
MPArithIntegers, rationals, floats, and complex numbersPascal, DelphiZlib
InfIntIntegersC++MPL
bigzIntegers, rationalsC (bindings for C++)BSD-type
C++ BigInt ClassIntegersC++GPL
rampIntegersRustApache License v2
floatFloatsRustApache License v2
fgmpIntegersCPublic Domain
imathIntegers, rationalsANSI CMIT
hebimathIntegers, rationals, naturals, floatsC (C99)MIT
bsdntIntegers, naturalsCBSD (2-clause)
integer-simpleIntegersHaskellBSD (3-clause)
bigintsIntegersNimMIT
libzahl (WIP)IntegersCISC
decimalDecimalsGoBSD (3-clause)
mpmathFloats and complexPythonBSD
Computable RealsComputable RealsCommon LispBSD (3-clause)
libmpdec and libmpdec++Decimal floatsC and C++BSD (2-clause)
GEM LibraryFloats and complex numbersMATLAB and GNU OctaveMPL
YorkWaugh
发表于 2022-11-25 22:44:09 | 显示全部楼层
本帖最后由 YorkWaugh 于 2022-11-25 23:07 编辑
带刀侍卫 发表于 2022-11-25 21:18
楼主想算底数=250,指数=250,结果是多少

提供一个很丑陋的解决方案
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main()
  5. {
  6.     char a[10];
  7.     int b = 0, x, d = 0, p[10000] = {}, q[10000] = {};
  8.     printf("请输入底数:\n");
  9.     gets(a);
  10.     for (unsigned int i = 0; i < strlen(a); i++)
  11.         b += (a[i] - 48) * pow(10, strlen(a) - i - 1);
  12.     printf("请输入指数:\n");
  13.     scanf("%d", &x);
  14.     for (unsigned int i = 0; i < strlen(a); i++)
  15.         p[strlen(a) - i - 1] = q[strlen(a) - i - 1] = a[i] - 48;
  16.     for (int i = 1; i < x; i++)
  17.     {
  18.         for (int j = 1; j < b; j++)
  19.             for (int k = 0; k < 10000; k++)
  20.                 if (p[k] + q[k] < 10)
  21.                     p[k] += q[k];
  22.                 else
  23.                     p[k + 1] += (p[k] + q[k]) / 10, p[k] = (p[k] + q[k]) % 10;
  24.         for (int j = 0; j < 10000; j++)
  25.             q[j] = p[j];
  26.     }
  27.     for (int i = 9999; i > 0; i--)
  28.         if (p[i] != 0)
  29.         {
  30.             d = i;
  31.             break;
  32.         }
  33.     printf("结果是:");
  34.     for (int i = d; i >= 0; i--)
  35.         printf("%d", p[i]);
  36.     putchar('\n');
  37.     return 0;
  38. }
复制代码

结果为
  1. 305493636349960468205197939321361769978940274057232666389361390928129162652472045770185723510801522825687515269359046715531785342780428396973513311420091788963072442053377285222203558881953188370081650866793017948791366338993705251636497892270212003524508209121908744820211960149463721109340307985507678283651836204093399373959982767701148986816406250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
复制代码
thelord
发表于 2022-11-26 08:00:34 | 显示全部楼层
改成循环,或者用数学库里的 pow函数(见2楼)
南山不会落梅花
 楼主| 发表于 2022-11-26 10:37:20 | 显示全部楼层
YorkWaugh 发表于 2022-11-25 20:24
不理解你的要求,这样可以吗

感谢
您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2024-11-24 02:04 , Processed in 0.130933 second(s), 16 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

快速回复 客服 返回顶部 返回列表