473,383 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Having trouble with printing and performing doubles (very basic)

1
So this:

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
double d = (1/6);
cout<<d;
return 0;
}

returns d as int, the fractional part is removed, can someone tell me why?
Dec 5 '13 #1
2 1038
stdq
94 64KB
Hi! When you calculate 1/6, you are performing integer division, because both 1 and 6 are integers (even though you assign the result to a double variable, is this case, d). To fix this, you could change 1 to 1.0, or 6 to 6.0, or both. Or, you could even perform a cast (a temporary conversion) in at least one of the operands. Cast to double to have more precision. Doing cast, you could do one of the following:

Expand|Select|Wrap|Line Numbers
  1. 1 / ( (double) 6 )
Expand|Select|Wrap|Line Numbers
  1. ( (double) 1 ) / 6
Expand|Select|Wrap|Line Numbers
  1. ( (double) 1 ) / ( (double) 6 )
So, you have 6 options to fix this issue. Bye!
Dec 5 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
The preferred style is:

1.0/6.0

These are doubles by definition. Using casts is very common in C but in C++ using them indicates a weakness in the code. You should never have to tell a C++ compiler what the type is.
Dec 6 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Stephen Horne | last post by:
Just recently I decided I want to make use of my ISP freebie webspace. In order to make that easier, I'd like to be able to automatically synchronise an FTP file/folder heirarchy with one on my...
1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
5
by: hellrazor | last post by:
Hi there, First of all, I'm very much a C++ amateur (i.e., a newb). I'm having to program a win32 system service for my employer, and I'm almost done with the task, but I need help with...
0
by: Ben Jacobs-Swearingen | last post by:
Hello, I just started learning C a couple weeks ago from Kernighan and Ritchie (first edition -- I can't afford the newer second edition), and have really enjoyed it so far. But I am having...
27
by: Ben Jacobs-Swearingen | last post by:
Hello, I just started learning C a couple weeks ago from Kernighan and Ritchie (first edition -- I can't afford the newer second edition), and have really enjoyed it so far. But I am having...
1
by: Jozef | last post by:
Hello. I'm having trouble creating a blank solution (and ASP.net web application) from my laptop. I own the server (in fact it's sitting right next to me) and have added the URL to the trusted...
5
by: tkondal | last post by:
Hi all. I just started looking at Python's ctypes lib and I am having trouble using it for a function. For starters, here's my Python code: from ctypes import*; myStringDLL=...
4
by: Stimp | last post by:
Hi all, I'm trying to read a particular node value from an XML file, but I've done some searching on the net and there doesn't seem to be a very basic example. Basically here's what I want to...
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.