14

In this code i got the above error in lines i commented.

public double bigzarb(long u, long v)
{
    double n;
    long x;
    long y;
    long w;
    long z;
    string[] i = textBox7.Text.Split(',');
    long[] nums = new long[i.Length];
    for (int counter = 0; counter < i.Length; counter++)
    {
        nums[counter] = Convert.ToInt32(i[counter]);
    }

    u = nums[0];
    int firstdigits = Convert.ToInt32(Math.Floor(Math.Log10(u) + 1));
    v = nums[1];
    int seconddigits = Convert.ToInt32(Math.Floor(Math.Log10(v) + 1));
    if (firstdigits >= seconddigits)
    {
        n = firstdigits;

    }
    else
    {
        n = seconddigits;        
    }
    if (u == 0 || v == 0)
    {
        MessageBox.Show("the Multiply is 0");
    }

    int intn = Convert.ToInt32(n);
    if (intn <= 3)
    {
        long uv = u * v;
        string struv = uv.ToString();
        MessageBox.Show(struv);
        return uv;
    }
    else
    {
        int m =Convert.ToInt32(Math.Floor(n / 2));

        x = u % Math.Pow(10, m); // here
        y = u / Math.Pow(10, m); // here
        w = v % Math.Pow(10, m); // here
        z = v / Math.Pow(10, m); // here

        long result = bigzarb(x, w) * Math.Pow(10, m) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(10, m) + bigzarb(y, z);///here
        textBox1.Text = result.ToString();
        return result;
    }
}

Whats is the problem? Thanks!

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Arash
  • 3,013
  • 10
  • 52
  • 74

6 Answers6

20

The Math.Pow method returns a double, not a long so you will need to change your code to account for this:

x = (long)(u % Math.Pow(10, m));

This code will cast the double result from Math.Pow and assign that value to x. Keep in mind that you will lose all the precision providided by decimal (which is a floating-point type and can represent decimal values). Casting to long will truncate everything after the decimal point.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
5

Change types

long x;
long y;
long w;
long z; 

to

double x;
double y;
double w;
double z; 

Or make use of

Convert.ToInt64
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • This will fix it, but this is a poor solution since he chose long for a reason and double types are the slowest type not counting Strings as a type. – EnabrenTane Dec 20 '10 at 13:03
5

Math.Pow returns a double.

the Right Hand Side (RHS) of % can only be an integer type.

you need

x = u % (long)Math.Pow(10, m);///<----here
y = u / (long)Math.Pow(10, m);///here
w = v % (long)Math.Pow(10, m);///here
z = v / (long)Math.Pow(10, m);///here

Additionally, You have the possibility of dividing by zero and destroying the universe.

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44
  • From your answer *the Right Hand Side (RHS) of % can only be an integer type*, but looking at this http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx I dont aggree... – Adriaan Stander Dec 20 '10 at 13:07
  • Read this sentence that the website you site says. Note the error for the types float and double. – EnabrenTane Dec 20 '10 at 13:09
  • 1
    It is referring to rounding *errors*, not compile/runtime errors. – Adriaan Stander Dec 20 '10 at 13:11
  • 3
    It would be better to cast the result of the modulus operation to `long` rather than just the result of `Math.Pow`. Also keep in mind that if the compiler is able to work out that either `u` or `v` is a floating point type then your code still will not compile since the result of the expression will still not be implicitly convertible to `long`. It's better to convert the result of the entire expression (as I have shown in my answer) to avoid all of these problems. – Andrew Hare Dec 20 '10 at 13:11
3

Math.Pow returns a double. You could explicitly cast to long, for example

x = u % (long)Math.Pow(10, m);

although that is likely not the correct solution. Are you certain that the results that you are after can be properly expressed as a double? If not then change the variables to be declared as doubles rather than longs.

John Pickup
  • 5,015
  • 2
  • 22
  • 15
  • Though this is general something one should consider whenever using a cast from `double` to `long`, I suspect in this special case `m` will always be small enough. – Doc Brown Dec 20 '10 at 13:09
2

Also you can use this:

Convert.ToInt64( u % Math.Pow(10, m) )

Source here

mayo
  • 3,845
  • 1
  • 32
  • 42
  • The advantage of using [`Convert.ToInt64(double)`](https://learn.microsoft.com/en-us/dotnet/api/system.convert.toint64?view=netframework-3.5#system-convert-toint64(system-double)) being that it is documented to return the value ***rounded** to the nearest 64-bit signed integer* rather than truncated as is done by the `(long)` cast. – dbc Jun 29 '23 at 21:28
2

You cant' cast implicitly double to long, use (long) cast or change type of variable declaration to double.

pyCoder
  • 503
  • 3
  • 9