strange calculation

This is a discussion on strange calculation within the C++ Programming forums, part of the General Programming Boards category; Hai, I am using Borland 3.0 DOS and windows XP. Well am expecting my program result 0.2 , on the ...

  1. #1
    void99
    Guest

    Smile strange calculation

    Hai,
    I am using Borland 3.0 DOS and windows XP. Well am expecting my program result 0.2 , on the contrary my program gives 0.00 as result. What is wrong with my prog.??? it looks fine for me.

    Thanks


    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<constream.h>
    #include<stdlib.h>
    #include<dos.h>
    #include<conio.h>
    #include<stdio.h>
    
    
    
    
    
         void main()
         {
          clrscr();
          double n;
    
          constream cout;
    
    
          n = 2+5 - 4*3 / 10 %2 -6;
    
          cout <<setxy(1,1) <<setiosflags(ios::showpoint) <<setw(10)
    	<< setprecision(3) <<n ;
    
    
          getch();
         }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > I am using Borland 3.0 DOS and windows XP
    You should really upgrade your compiler

    > void main()
    Yeah right
    Use int main
    As endless posts / FAQs will tell you


    > n = 2+5 - 4*3 / 10 %2 -6;
    Its an integer expression, so everything truncates to 0

    Say perhaps
    n = 2.0+5.0 - 4.0*3 / 10 %2 -6.0;

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    hmm

    4*3=12
    12/10=1.2
    1.2%2=0 R 1.2
    2+5=7
    7-1.2=5.8
    5.8-6=-.2

    hmm
    curious
    Couldn't find the operator precedence for % but I am assuming it's L-R and the same as * (multiplcation) and division.
    I suggest using () to make the equation crystal clear.

  4. #4
    Shadow12345
    Guest
    try using float instead of double perhaps?

  5. #5
    Shadow12345
    Guest

    constream.h?

    the header constream.h isn't included with Visual Studio or Borland 5.02

    You really should upgrade your compiler so other people can help you.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    3
    I would suggest using float and use braces to clearly write out your equation. It might also be easier if you perform the calculations in different parts.

    float a,b,c;

    a = 3.0*5.5;
    b = 2.2/1.1;

    c = a/b;

    cout<<c<<endl;

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > 4*3=12
    > 12/10=1.2
    Actually, its 12/10 = 1
    It's all integer arithmetic at this point - its only at the assignment that the whole thing gets converted to a double.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 11:51 PM
  3. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 06:37 AM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 11:21 AM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21