Code:
//------------------------------------------------------------------
// test_rest
// vergleich eigene funktionen und fmod
// restfunktionen für uint16_t und für int16_t
//------------------------------------------------------------------
#include <math.h>
#include "asuro.h"
//------------------------------------------------------------------
// Restberechnung für Zahlen zwischen 1 und 65535
uint16_t RestUint(uint16_t dividend, uint16_t divisor)
{
uint16_t rest;
if (divisor != 0)
{
rest = dividend - (dividend/divisor * divisor);
}
else
{
SerWrite ("ERROR! ", 7);
rest = 0;
}
return (rest);
}
//------------------------------------------------------------------
// Restberechnung für Zahlen zwischen -32768 und 32767
int16_t RestInt(int16_t dividend, int16_t divisor)
{
int16_t rest;
if (divisor != 0)
{
rest = dividend - (dividend/divisor * divisor);
}
else
{
SerWrite ("ERROR! ", 7);
rest = 0;
}
return (rest);
}
//------------------------------------------------------------------
void PrintUint (uint16_t wert)
{
char text [12];
ltoa (wert, text, 10);
SerPrint (text);
}
//------------------------------------------------------------------
void PrintRestUint (uint16_t dividend,uint16_t divisor)
{
float fmodrest;
uint16_t rest;
rest = RestUint(dividend,divisor);
fmodrest = fmod (dividend, divisor);
PrintUint (dividend);
SerWrite ("/", 1);
PrintUint (divisor);
SerWrite ("=", 1);
PrintUint (dividend/divisor);
SerWrite (" ", 2);
PrintUint ((dividend/divisor)*divisor);
SerWrite (" rest: ", 8);
PrintUint (rest);
SerWrite (" fmod: ", 8);
PrintFloat (fmodrest, 5, 2);
SerWrite ("\r\n", 2);
}
//------------------------------------------------------------------
void PrintRestInt (int16_t dividend,int16_t divisor)
{
float fmodrest;
int16_t rest;
rest = RestInt(dividend,divisor);
fmodrest = fmod (dividend, divisor);
PrintInt (dividend);
SerWrite ("/", 1);
PrintInt (divisor);
SerWrite ("=", 1);
PrintInt (dividend/divisor);
SerWrite (" ", 2);
PrintInt ((dividend/divisor)*divisor);
SerWrite (" rest: ", 8);
PrintInt (rest);
SerWrite (" fmod: ", 8);
PrintFloat (fmodrest, 5, 2);
SerWrite ("\r\n", 2);
}
//------------------------------------------------------------------
int main(void)
{
Init ();
StatusLED(YELLOW);
SerWrite ("RestUint:\r\n", 11);
PrintRestUint (100, 6);
PrintRestUint (100, 0);
PrintRestUint (12345, 233);
PrintRestUint (32767, 12345);
PrintRestUint (65535, 123);
PrintRestUint (123, 1234);
SerWrite ("RestInt:\r\n", 11);
PrintRestInt (100, 5);
PrintRestInt (100, 0);
PrintRestInt (12345, 233);
PrintRestInt (32767, -12345);
PrintRestInt (-32768, 539);
PrintRestInt (-345, -123);
PrintRestInt (123, 1234);
//------------------------------------------------------------------
StatusLED (RED);
BackLED (ON, ON);
while (1);
return 0;
}
Gruss
Lesezeichen