Taskulaskimet tulostavat kertoman (kts. wikipedia) usein pelkästään lukuun 69 asti, koska tuloksessa ei voi olla enempää kuin 99 nollaa. Äärevät luvut ovat siis laskimille pulma, mutta ongelman voi kiertää kun avuksi ottaa ohjelman jossa luku on purettu alkutekijöihinsä eli mantissaan ja exponenttiin. Mantissa tarkoittaa luvun ensimmäisiä desimaaleja, exponentti sitä kuinka monta kertaa se pitää kertoa kymmenellä.
Em. idea c-kielellä toteutettuna näyttää tältä:
//kertoma.c: kertoma suuresta luvusta iteraatiomenetelmällä toteutettuna.
#include
double purku(double ,long int * e);//paluuarvot: mantissa(float) ja exponentti(int*)
struct liuku { //desimaalilukuja alkutekijöissään
double m; //koko 8 tavua
unsigned long int e; //koko 4 tavua
}x,y;
int main(int argc, char *argv[]) //luetaan luku komentoriviltä
{
int b=atoi(argv[1]); //muutetaan luku merkkijonosta kokonaisluvuksi
if (argc!=2){return 1;} //virhetilanteen hallinta
double a=1;
x.m=1;x.e=0; //kertoman siemenluku
for(a=1;a=10){
x.m=x.m/10;
x.e ;
}
}
a--;
printf("Luvun %g kertoma on %8.7g E%u\n",a,x.m,x.e);
return 0;
}
double purku(double a, long int * pe) //puretaan desimaaliluku alkutekijöihinsä
{
long int e=0;
if(a>1){
while(a>1){a=a/10;e ;}
}
if(a
Kertoman laskeminen
3
2250
Vastaukset
Edellä mainittu ohjelma laskee kertoman vain likimäärin mutta tämä laskee sen absoluuttisen tarkasti. Ohjelma perustuu binäärikoodattuun desimaalilukuun (bdc) jolla kierretään muutujan kokorajoitukset. Se miten suuresta luvusta voi kertoman ottaa riippuu taulukkomuuttujan ak[] koosta kun koko on 1000 alkiota voidaan ottaa kertoma luvusta 470.
//kertoma3.c: Absoluuttisen tarkka kertoman laskenta äärevästä luvusta.
#include
char ak[1000]={0}; //bdc_luku (kokonaisluku)
void kertoma(int n);
void kerto(int n); //bdc_kertolasku
void tulostus(void); //
int main()
{
int n=470;
kertoma(n);
printf("%i! = ",n);tulostus();printf("\n");
return 0;
}
void kertoma(int n)
{
int i=1;
ak[999]=1; //kertoman siemenluku
for(i=1;i=0;i--){
yli=(ak[i]*n yli)/10;
ak[i]=(ak[i]*n m)%10;
m=yli;
}
}
void tulostus()
{
int i=0,j=0, rp=0;
for(i=0;i- koodiApina
code.c: In function 'main':
code.c:11: error: parameter 'n' is initialized
code.c:12: error: expected declaration specifiers before 'kertoma'
code.c:13: error: expected declaration specifiers before 'printf'
code.c:13: error: expected declaration specifiers before 'tulostus'
code.c:13: error: expected declaration specifiers before 'printf'
code.c:14: error: expected declaration specifiers before 'return'
code.c:18: error: parameter 'i' is initialized
code.c:19: error: expected declaration specifiers before 'ak'
code.c:20: error: expected declaration specifiers before 'for'
code.c:20: error: expected declaration specifiers before 'i'
code.c:20: error: expected declaration specifiers before 'i'
code.c:18: error: declaration for parameter 'i' but no such parameter
code.c:16: error: declaration for parameter 'kertoma' but no such parameter
code.c:11: error: declaration for parameter 'n' but no such parameter
- GMP-fani
GMP:llä helpommin. http://www.cs.colorado.edu/~srirams/classes/doku.php/gmp_usage_tutorial . En tiedä, miten tuo käsittelee ylivuototilanteet.
Ketjusta on poistettu 1 sääntöjenvastaista viestiä.
Luetuimmat keskustelut
- 396895
- 323888
- 453270
- 342844
- 162662
- 372228
- 162176
- 372082
Voi ei! Jari Sillanpää heitti keikan Helsingissä - Hämmästyttävä hetki lavalla...
Ex-tangokuningas on parhaillaan konserttikiertueella. Hän esiintyi Savoy teatterissa äitienpäivänä. Sillanpää jakoi kons482037- 371952