Hi. I am programmer who works with c# and c++. These languages are really powerful but when it comes to controlling memory I can never do what I want to do. I want to know if there's a lower level languge (or a way to do this with c# or c++) which gives you more freedom and lets you control memory and make your own types. This can be really useful for working with complicated math algorithms. I just don't want to be limited by the pre-defined types. For example, I want to make a 1kb integer or one that is dynamic and has no limits. Or how would I do this:
I want to divide two numbers and get a result which constantly increases its accuracy. So if you divide, for example, 1 by 3, you'll get 0.33333 in the first couple of milliseconds, then 0.33333333333 and so on without stopping. Or if I want to make an integer which has no limits and you can add to it as much as you want.

Thank you.

Recommended Answers

All 7 Replies

C# and C++ can both do these things, it is just a matter of how much work you are willing to put into writing the classes.

C# and C++ can both do these things, it is just a matter of how much work you are willing to put into writing the classes.

And how on earth would I write those classes?

with your fingers

-sorry couldn't help it

with your fingers

-sorry couldn't help it

Thanks. I've marked the thread as solved.

> And how on earth would I write those classes?
The same way you write any program.

  1. Define the problem
  2. Design a solution to the problem
  3. Write code to implement the solution

Edward cannot suggest how to write anything with the vague descriptions you have given. This stems from step #1; the problem has not been adequately defined, and so designing a solution is impossible.

> And how on earth would I write those classes?
The same way you write any program.

  1. Define the problem
  2. Design a solution to the problem
  3. Write code to implement the solution

Edward cannot suggest how to write anything with the vague descriptions you have given. This stems from step #1; the problem has not been adequately defined, and so designing a solution is impossible.

All I wanted to know if there's a quick way to do this so that if there is I wouldn't have to write unecessary code.

I just don't want to be limited by the pre-defined types.

You can define your own types easily enough, no?

For example, I want to make a 1kb integer or one that is dynamic and has no limits.

You're contradicting yourself. A 1kB integer has limits. An integer that is dynamic and has no limits won't always be 1kB.

And this (an integer type with no limits) is easy enough to implement in C# or C++, and you don't even have to use pointers. Here are some straightforward skeletons -- adding signedness and other operators, and fixing whatever bugs accidentally left in this code, are left as an exercise to the reader.

C#:

class BigInt {
  UInt32[] data;

  private BigInt(UInt32[] data) {
    this.data = data;
  }

  public BigInt(UInt32 x) {
    data = new UInt32[] { x };
  }

  public static BigInt operator+(BigInt x, BigInt y) {
    int xn = Length(x.data);
    int yn = Length(y.data);
    int n = xn < yn ? yn + 1 : xn + 1;
    UInt32[] ret = new UInt32[n];
    bool carry = false;
    for (int i = 0; i < n; ++i) {
      UInt32 xd = i < xn ? x.data[i] : 0;
      UInt32 yd = i < yn ? y.data[i] : 0;
      UInt32 xd_yd = xd + yd;
      UInt32 s = xd_yd + (carry ? 1 : 0);
      ret[i] = s;
      carry = (xd_yd < xd || s < xd_yd);
    }
    return new BigInt(ret);
  }

  public static BigInt operator*(BigInt x, BigInt y) {
    int xn = Length(x.data);
    int yn = Length(y.data);
    int n = xn + yn;
    UInt32 ret = new UInt32[n];
    for (int i = 0; i < xn; ++i) {
      UInt32 carry = 0;
      for (int j = 0; j < yn; ++j) {
        UInt64 xd = x.data[i];
        UInt64 yd = y.data[i];
        UInt64 xy = xd * yd + ((UInt64)ret[i+j]) + carry;
        ret[i+j] = (UInt32)xy;
        carry = (UInt32)(xy >> 32);
      }
      int k = yn;
      while (carry > 0) {
        UInt32 s = ret[k] + carry;
        carry = (s < carry ? 1 : 0);
        ret[k] = s;
        ++k;
      }
    }
  }

  private static bool Length(UInt32[] x) {
    int i = x.Length;
    while (i > 0 && x[i-1] != 0) {
      --i;
    }
    return i;
  }
}

C++:

class bigint {
  std::vector<uint32_t> data;
 public:
  bigint() { }
  bigint(uint32_t x) {
    if (x > 0) {
      data.push_back(x);
    }
  }

  

  bigint operator+(bigint const& y) const {
    bigint r;
    int n = max(data.size(), y.data.size());
    uint32_t carry = 0;
    for (int i = 0; i < n; ++i) {
      uint32_t xd = i < data.size() ? data[i] : 0;
      uint32_t yd = i < y.data.size() ? y.data[i] : 0;
      uint32_t xd_yd = xd + yd;
      uint32_t s = xd_yd + carry;
      r.data.push_back(s);
      carry = (xd_yd < xd || s < xd_yd);
    }
    return r;
  }
 private:
  void trim() {
    int n = data.size();
    while (n > 0 && data[n-1] == 0) {
      --n;
    }
    data.resize(n);
  }

};

Or how would I do this:
I want to divide two numbers and get a result which constantly increases its accuracy. So if you divide, for example, 1 by 3, you'll get 0.33333 in the first couple of milliseconds, then 0.33333333333 and so on without stopping.

You can do that in both C# and C++ (and basically any language). What makes you think you can't?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.