www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why am I getting a dividing by zero error message

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
Here is the output/input of the program:
Type in  data for an egg:
Width: 3
Hight: 2

object.Error (0): Integer Divide by Zero
----------------
0x004023FE
0x0040CF9F
0x0040CF19
0x0040CDB4
0x00409033
0x00402638
0x75F86359 in BaseThreadInitThunk
0x77018944 in RtlGetAppContainerNamedObjectPath
0x77018914 in RtlGetAppContainerNamedObjectPath
Chicken:

Here is the source code:

import std.stdio;
import std.string;
void main(){
     egg[1000] data;
     data[0] = (new egg(0,0,"a"));
     for(int i = 1;i<1000;i++)
     {
         int tempx;
         int tempy;
         string tempz;
         int high = 0;
         double highs = 0;
         writeln("Type in  data for an egg:");
         write("Width: ");
         readf(" %d",&tempx);
         write("Hight: ");
         readf(" %d",&tempy);
         write("Chicken: ");
         tempz = readln();
         data[i] = new egg(tempx,tempy,tempz);
         for(int x = 0;x < i;x++)
         {
             int tempa;
             int tempb;
             double temp;
             tempa = data[x].x;
             if(tempa < 0)
                 tempa-=tempa;
             tempb = data[x].y;
             if(tempb < 0)
                 tempb-=tempb;
             temp = tempa / tempb;
             if(temp > highs)
             {
                 highs = temp;
                 high = x;
             }
         }
         tempx = data[high].x - data[i].x;
         if(tempx < 0)
             tempx-=tempx;
         tempy = data[high].y - data[i].y;
         if(tempy < 0)
             tempy-=tempy;
     }
}
class egg {
public:
     this(uint x,uint y,string name)
     {
         this.x = x;
         this.y = y;
         name = cast(string)name;
     }
     uint x;
     uint y;
     char[] name;
}

Why is this happening? Does anybody know?
Jan 28 2021
next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:
 Here is the output/input of the program:
 Type in  data for an egg:
 Width: 3
 Hight: 2

 [...]
It might help to separate break this out into smaller functions. May make it easier to follow what is happening.
Jan 28 2021
prev sibling next sibling parent reply Dennis <dkorpel gmail.com> writes:
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:
 object.Error (0): Integer Divide by Zero
 Why is this happening? Does anybody know?
 data[0] = (new egg(0,0,"a"));
Here you set data[0].y to 0
 tempb = data[x].y;
In the first iteration, this equals data[0].y which equals 0
 temp = tempa / tempb;
And then you divide by zero here, hence the error
Jan 28 2021
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Thursday, 28 January 2021 at 18:53:51 UTC, Dennis wrote:
 On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
 wrote:
 object.Error (0): Integer Divide by Zero
 Why is this happening? Does anybody know?
 data[0] = (new egg(0,0,"a"));
Here you set data[0].y to 0
 tempb = data[x].y;
In the first iteration, this equals data[0].y which equals 0
 temp = tempa / tempb;
And then you divide by zero here, hence the error
Okay. That worked. I added a check to set temp to zero if tempa or tempb is zero.
Jan 28 2021
parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Thursday, 28 January 2021 at 18:56:45 UTC, Ruby The Roobster 
wrote:
 On Thursday, 28 January 2021 at 18:53:51 UTC, Dennis wrote:
 On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The 
 Roobster wrote:
 object.Error (0): Integer Divide by Zero
 Why is this happening? Does anybody know?
 data[0] = (new egg(0,0,"a"));
Here you set data[0].y to 0
 tempb = data[x].y;
In the first iteration, this equals data[0].y which equals 0
 temp = tempa / tempb;
And then you divide by zero here, hence the error
Okay. That worked. I added a check to set temp to zero if tempa or tempb is zero.
Instead of dividing, which causes the bug.
Jan 28 2021
prev sibling next sibling parent evilrat <evilrat666 gmail.com> writes:
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:
 Here is the output/input of the program:
 Type in  data for an egg:
 Width: 3
 Hight: 2

 object.Error (0): Integer Divide by Zero
 ----------------
 0x004023FE
 0x0040CF9F
 0x0040CF19
 0x0040CDB4
 0x00409033
 0x00402638
 0x75F86359 in BaseThreadInitThunk
 0x77018944 in RtlGetAppContainerNamedObjectPath
 0x77018914 in RtlGetAppContainerNamedObjectPath
 Chicken:

 Here is the source code:

 import std.stdio;
 import std.string;
 void main(){
     egg[1000] data;
     data[0] = (new egg(0,0,"a"));
     for(int i = 1;i<1000;i++)
     {
         int tempx;
         int tempy;
         string tempz;
         int high = 0;
         double highs = 0;
         writeln("Type in  data for an egg:");
         write("Width: ");
         readf(" %d",&tempx);
         write("Hight: ");
         readf(" %d",&tempy);
         write("Chicken: ");
         tempz = readln();
         data[i] = new egg(tempx,tempy,tempz);
         for(int x = 0;x < i;x++)
         {
             int tempa;
             int tempb;
             double temp;
             tempa = data[x].x;
             if(tempa < 0)
                 tempa-=tempa;
             tempb = data[x].y;
             if(tempb < 0)
                 tempb-=tempb;
/* x starts with 0, you are acessing data[x] which is set to egg(0,0,"a") and you get div by zero as a result. I see logic error, though I might be wrong because I haven't actually run your code. */
             temp = tempa / tempb;
             if(temp > highs)
             {
                 highs = temp;
                 high = x;
             }
         }
         tempx = data[high].x - data[i].x;
         if(tempx < 0)
             tempx-=tempx;
         tempy = data[high].y - data[i].y;
         if(tempy < 0)
             tempy-=tempy;
     }
 }
Jan 28 2021
prev sibling parent tsbockman <thomas.bockman gmail.com> writes:
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:
 Here is the output/input of the program:
 Type in  data for an egg:
 Width: 3
 Hight: 2

 object.Error (0): Integer Divide by Zero
 ...

 Here is the source code:

 import std.stdio;
 import std.string;
 void main(){
     egg[1000] data;
     data[0] = (new egg(0,0,"a"));
     for(int i = 1;i<1000;i++)
     {
         ...
         data[i] = new egg(tempx,tempy,tempz);
         for(int x = 0;x < i;x++)
         {
             int tempa;
             int tempb;
             double temp;
             tempa = data[x].x;
             if(tempa < 0)
                 tempa-=tempa;
             tempb = data[x].y;
             if(tempb < 0)
                 tempb-=tempb;
             temp = tempa / tempb;
             if(temp > highs)
             {
                 highs = temp;
                 high = x;
             }
         }
 ...

 Why is this happening? Does anybody know?
1) Outside the loops, you set `data[0] = (new Egg(0, 0, "a"))` 2) The outer `for(int i` loop, starts at `i = 1`, and so it never overwrites the work of step (1). 3) The inner `for(int x` loop starts at `x = 0`, so its first pass divides by `data[1].y`. Since you set that field equal to 0 in step (1), and both operands have type `uint`, the result is a divide by zero error. 4) Also, `if(tempb < 0) tempb-=tempb;` will convert any negative values into zeroes. Maybe you meant to write `if(tempb < 0) tempb = -tempb;` to get the absolute value, instead? There are many ways you could fix the program. (I won't try to guess what the best way is, though, because it's not entirely clear to me what this program is actually intended to do. As is, it does some calculations, but doesn't try to output their results in any way.)
Jan 28 2021