Einzelnen Beitrag anzeigen

gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#28

AW: Welcher Befehl rundet wie "Round" in Borland C++ ?

  Alt 22. Nov 2010, 16:05
1. Wie funktioniert Delphi.Round(); (was steckt dahinter) um dies in BorlandC++.Fkt() umzuschreiben ?
round rundet zum nächste Integer unter Verwendung des eingestellten Rundungsmodus. Standard-C hat dafür
Code:
int lrint(double x);
Allerdings scheint BorlandC++ genauso wenig Standard, wie Delphi Standard-Pascal ist.
2. Bitte um Beispiele mit Sourcecode
Hier notfalls ein Round-To-Even:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

long int roundtoeven(double x) {
  double y;
  long l;
  y = 2.0*x;
  if (floor(y)==y) {
    l = (long) y;
    if ((l & 3) == 3) l++;
    l = l >> 1;
  }
  else {
    l = floor(x+0.5);
  }
  return l;
}


int main (void) {
  double x;
  long int l,i;

  for (i=-50;i<=100;i++) {
    x = 0.1*i;
    l = roundtoeven(x);
    printf("%f %d\n", x, l);
  }

  return 0;
}
  Mit Zitat antworten Zitat