ChaGPT Arduino’ da uzun bölme yapan program yazdı

void setup() {
   Serial.begin(9600);
}

void loop() {
   long dividend = 12345;
   long divisor = 123456;
   int decimal_places = 1000;
   
   char result[decimal_places+2]; // +2 for decimal point and null terminator
   memset(result, 0, sizeof(result)); // initialize result to all zeroes
   
   // Handle negative values
   bool negative = false;
   if ((dividend  0) || (dividend > 0 && divisor < 0)) {
     negative = true;
   }
   dividend = abs(dividend);
   divisor = abs(divisor);
   
   // Calculate integer part of quotient
   long quotient = dividend / divisor;
   ltoa(quotient, result, 10);
   
   // Add decimal point
   strcat(result, ".");
   
   // Calculate decimal part of quotient
   long remainder = dividend % divisor;
   for (int i = 0; i < decimal_places; i++) {
     remainder *= 10;
     quotient = remainder / divisor;
     remainder = remainder % divisor;
     result[i+2] = quotient + '0';
   }
   
   // Print result
   if (negative) {
     Serial.print("-");
   }
   Serial.println(result);
   
   while (1) {} // stop program from continuing indefinitely
}
Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.