Integer Rundungsfehler vermeiden

Gast #2186157
Lesenswert?

Wie vermeidet man sowas:
1
#include "stdint.h"
2
#include "stdio.h"
3
int main(void)
4
{
5
uint8_t A = 10;
6
uint8_t B = 2;
7
uint8_t n = 5;
8

9
int8_t steps = (B - A) / n;
10
int8_t pos = A;
11

12
printf( "From %d to %d (%d steps) in %d increments of %d\n", A, B, A-B, n, steps );
13

14
 while( n-- )
15
 {
16
  printf( "%d %d\n", n, pos );
17
  pos += steps;
18
 }
19

20
return 0;
21
}
1
From 10 to 2 (8 steps) in 5 increments of -1
2
4 10
3
3 9
4
2 8
5
1 7
6
0 6

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren