-1

What am I trying to do?

I am trying to calculate the equivalent resistance of this resistance setup:

Circuit example

R1, R2, R3, R4 are of type int given by the user. The computer should compute the equivalent resistance Req of type int rounding it to the nearest integer.

What have I tried? I have tried rounding using the ceil function for rounding.

#include <iostream>
#include <cmath>

int main(){
  
  double R1, R2, R3, R4;
  
  std::cout << "enter R1: " << std::endl;
  std::cin >> R1; 
  std::cout << "enter R2: " << std::endl;
  std::cin >> R2; 
  std::cout << "enter R3: " << std::endl;
  std::cin >> R3; 
  std::cout << "enter R4: " << std::endl;
  std::cin >> R4; 
  
  double R12;
  R12 = R1 + R2;
  double R34;
  R34 = R3 + R4;
  
  int Req;
  Req = ceil((R12*R34)/(R12+R34));
  
  std::cout << "the equivalent resistance is: " << Req << std::endl;
  
  return 0;
}

What's the issue? I wanted to try doing it without using double type variables, but if I turn R12 and R34 into int, then ceil doesn't work anymore.

Why isn't this question a duplicate? I only found questions similar to this one but they all have a major drawback: Int overflowing, which in my case is likely to happen in case the resistance is entered in mOhm, so to speak.

Emilio
  • 102
  • 11
  • declare `R12` and `R34` to be a `double` and you won't truncate the sums. To round to the nearest integer, use `std::round(x);` – Stack Danny Sep 28 '22 at 12:35
  • That works, Thanks! But is there also a way to do it without floating point variables? – Emilio Sep 28 '22 at 12:39
  • This doesn't address the question, but instead of `int R12; R12 = R1 + R2;` use `int R12 = R1 + R2;`. For builtin types the two are the same, but when you're using objects that are expensive to create, you don't want the overhead of that create-then-assign two-step. – Pete Becker Sep 28 '22 at 12:41
  • I mean you could multiple for example all your inputs by a 1000 (i.e. go from volts to milli volts or wtv unit youre in) and and then divide back at the end but you risk overflow issues. Not sure why you want to avoid float/double. – Borgleader Sep 28 '22 at 12:42
  • I realise this isn't answering the question as asked, but I wonder why you need to calculate the equivalent resistance with rounding up to the nearest integer in the first place. Replacing the four resistors with a single resistor that has a greater (equivalent) resistance [or vice versa] can compromise behaviour of circuits. There is also nothing wrong or unusual with a circuit (or single resistor) having a non-integral resistance. – Peter Sep 28 '22 at 13:04
  • @Peter there aren't many circuits that would be sensitive to a fraction of an ohm change in resistance nor many resistors who's value tolerances wouldn't produce a larger change. Anyway this is a programming question rather than an electronics one – Alan Birtles Sep 28 '22 at 13:45
  • @AlanBirtles Thanks for suppressing curiosity about why a question was asked in the first place. – Peter Sep 28 '22 at 14:18

1 Answers1

3

To do it with integer types you have to go through two steps. First, do the division. Second, check the remainder to see whether the truncated result needs to be adjusted.

int result = (R12 * R34) / (R12 + R34);
int rem = (R12 * R34) % (R12 + R34);
if (2 * rem >= R12 + R34)
    ++result;

If you write code like that in a real-world project, expect to get funny looks from your co-workers.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165