[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] C og regex



On Sun, 14 Mar 2004 16:27:11 +0100, Erwin S. Andreasen wrote:

> Christian Nielsen <sslug@sslug> writes:
> 
>> Er programmerer normalt i PERL, så C er noget fremmed.
>> Desværre regner PERL forkert, hvis man lægger mange små tal sammen.
> 
> Det er næppe en fejl i Perl (ikke PERL) men skyldes sikkert at du
> bruger "floating point" tal. Perl's tal er det samme som "double" i C,
> så at skifte til C vil næppe hjælpe (med mindre du fx bruger "long
> double", men den er ikke meget mere præcis).
> 
> Læs fx http://docs.sun.com/source/806-3568/ncg_goldberg.html for lidt
> om hvordan "floating point" fungerer og hvilke begrænsninger det har.
> 
> Hvis du vil lave beregninger med mere præcision kan du bruge fx GNU
> MP biblioteket. Den har endda rationelle tal, så du kan lave
> beregninger uden at tabe præcision overhovedet (men det bliver langsommere).
> 
> Der er ingen grund til at skifte væk fra Perl endnu; der findes
> bindinger for GMP for Perl. De fleste af dine beregninger kan så
> holdes i GMP's hurtigere C kode.

Tak. Jeg kender resultatet af mime beregninger, så derfor ved jeg at Perl
regner forkert. Den samme kode oversat til C regner rigtigt. Jeg har
arbejdet noget videre på programmet, så jeg har ikke længere den
direkte oversættelse, men jeg har vedlagt det som giver problemer.
Resultatet af integralet er 3. C-programmet regner mere ud.
// Chr.

#!/usr/bin/perl
                                                                               
my $p=36;
my $h=0.00001;
my $n=1;
my $offset=0;
my $flag=0;
my $Ai=0;
my $A=0;
my $punkt3=0;
                                                                               
while ($flag==0) {
  $punkt3=$offset+($n*$h-($h/2));
  $punkt2=$offset+($n*$h);
  $Ai=(-($punkt3^2)+10*$punkt3)*$h;
  $A=$Ai+$A;
  if (($p-$A)<0.001) { $flag=1 }
  $n=$n+1;
}
print "A-p $A-$p, $punkt2\n";

int main()
{
  float arealarray[MAX_FRACTIONS];
  fill_array(arealarray, -1);
  arealarray[0] = 36;
  arealarray[1] = 72;
  arealarray[2] = 54;
  int areal=36;
  float skridt=0.00001;
  int sojlenr=0;
  float offset=0;
  float nyoffset=0;
  int flag=0;
  float Ai=0;
  float A=0;
  float punkt=0;
  int i=0;
                                                                                                                                            
  puts("Programmet kører nu...\n");
  for (i=0;i<=3;i++){
    printf("Ny fraktion:%5.2f\n",arealarray[i]);
    while (flag==0 && (25>(sojlenr*skridt))) {
      sojlenr=sojlenr+1;
      punkt = offset+sojlenr*skridt-skridt/2;
      Ai = (-punkt*punkt+10*punkt)*skridt;
      A = A + Ai;
      if ((arealarray[i]-A)<0.001) {
        flag=1;
      }
      nyoffset=offset+sojlenr*skridt;
    }
    A=0;
    Ai=0;
    flag=0;
    printf("Nyt offset %5.2f\n", nyoffset);
                                                                                                                                            
  }
  printf("Funktionskald: %5.2f\n", cal_fdis(arealarray));
  return 0;
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *