-1

So guys i have a homework to do but its not the case, i have a problem with double variable because it is calculated without floating point like an integer

I've tried writing (double) before calculation but it didn't work

    #include <iostream>
    #include <cstdlib>
    #include <iomanip>

    using namespace std;

    int main () {

        system ("chcp 1257");
        system ("color 8F");

        for(int a = 0 ;a <=18;a++){

            double t = 8 + (18 - a)/2;
            cout << "\n       "<<setw(2)<< a << "                " << setprecision(1) << fixed << setw(4)<< t ;
        };


    return 0;
    }

The result that i get is this https://i.stack.imgur.com/Lpc2R.png

even though i should get this https://i.stack.imgur.com/reAKa.png

(it's my first post so i can't format images like normal people do)

Edit: The problem was that (double) does not convert all variables in the line of code and variables that are needed for conversion are those that are needed to be divided

Denis_LT
  • 118
  • 7
  • 2
    `8 + (18 - a)/2` only involves `int`s, hence only integer operations are used, including the division. – Quentin Apr 11 '19 at 18:36
  • 5
    You don't need images to share your expected and actual output. Anyone who uses a screen reader won't be able to read them now. Anyone who has imgur links blocked also won't be able to see them. If imgur gets ddosed, no one will be able to see them during that time. It's courteous to paste them as snippets of text. – chris Apr 11 '19 at 18:37
  • 2
    `8 + (18.0 - a)/2;` would do it. The reasons (and this idiom) are crystal clear to a professional programmer, but (forgive me for being patronising), you might want to add a supporting comment. – Bathsheba Apr 11 '19 at 18:38
  • Wow thanks only a few second passed for my post and my problem is already solved – Denis_LT Apr 11 '19 at 18:39
  • We're good here you know ;-) – Bathsheba Apr 11 '19 at 18:39
  • But before you ask your next question would you mind taking the site tour? – Bathsheba Apr 11 '19 at 18:40
  • But Quentin even if i convert numbers into double like this double t =(double) 8 + (18 - a)/2; values are still wrong – Denis_LT Apr 11 '19 at 18:40
  • @Denis_LT `(18 - a)/2` is evaluated separately from `(double)8`, they are two operands of the `+` operator. Their types determine the type of the result but has no direct effect on how they are each evaluated. – François Andrieux Apr 11 '19 at 18:40
  • @Denis_LT: That's because you promoted the wrong term. Read K & R very carefully. – Bathsheba Apr 11 '19 at 18:40
  • @FrançoisAndrieux: That's not really technically correct. – Bathsheba Apr 11 '19 at 18:41
  • 1
    @Bathsheba My FGITW blew a blank. My bad. Mobile SO is hard. – Quentin Apr 11 '19 at 18:43

1 Answers1

1

Disclaimer: I am not a computer scientist so I can not accurately explain how numbers are represented in the computer or how "floating point arithmetic" is done. However, I recognise your conceptual problem from my own early days.

  • Consider first an example python 2:

    usr@cmptr $ python2
    >>> 1/2
    0
    

    That is obviously not right...

  • What about in python3 then?

    usr@cmptr python3
    >>> 1/2
    0.5
    
  • Hmmmm, how about R?

    usr@cmptr $ R
    > 1/2
    [1] 0.5
    
  • ok... and octave/matlab

    usr@cmptr $ octave --no-gui
    octave:1> 1/2
    ans =  0.50000
    

    What is going on here?!

  • When in doubt go back to the source, which is FORTRAN:

    ! File name: "main.f90"
    program min
    x =   1/2
    y = 1.0/2
    print *, x, y
    end program main
    

    Compile and run:

    usr@cmptr $ gfortran main.f90 -o main
    usr@cmptr $ ./main
    0.00000000      0.500000000
    

    The conclusion now is that there is obviously a difference between how we handle division of integers and decimal numbers...

  • To illustrate use good old C.

    // file name: main.c
    #include <stdio.h>
    int main()
    {
      int i_a, i_b;          // <-- Integer versions of "a" and "b"
      i_a = 1; i_b = 2;
      double d_a, d_b;       // <-- Decimal versions of "a" and "b"
      d_a = 1; d_b = 2;
      printf(                 //     Print to screen
        "%d, %f, %f, %f, %f, %d\n",
        i_a/i_b,             // <-- regular integer division
        d_a/d_b,             // <-- regular floating point division
        (double)i_a/i_b,      // <-- cast "i_a" to a decimal number
        i_a/(double)i_b,      // <-- cast "i_b" to a decimal number
        1.0/2,               // <-- define 1.0 as a decimal number
        1/2                  // <-- regular integer division
      );
      return 0;
    }
    

    Now compile and run:

    usr@cmptr $ gcc     main.c   -o main
    usr@cmptr $ ./main
    0, 0.500000, 0.500000, 0.500000, 0.500000, 0
    

Is seems like we need at least one number to be a decimal number, or "float", in order to achieve the desired result. The reason is that division of integers is handled differently than decimal numbers.

I hope this helps.

Sigve Karolius
  • 1,356
  • 10
  • 26