473,406 Members | 2,208 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,406 software developers and data experts.

A function that takes an int but returns a double

bluechimera
hi,
I am having a problem getting the integer I pass into a function to return as double. it is still truncating the value on the return. any suggestions?


watered down version of my code
Expand|Select|Wrap|Line Numbers
  1.  
  2. int choice = 0;
  3. {
  4. cout << "Enter a number: ";
  5. cin >> choice;
  6.  
  7. cout << "The new number is: " << endl;
  8.  
  9. return 0;
  10. }
  11.  
  12.  
  13. double oneDivide(int n)
  14. {
  15. return 1/n;
  16. }
  17.  
  18.  
I keep getting zero as the result (truncating the decimal and value after decimal. I cannot use any local variables
Any suggestions ?
Thank you!
Jan 28 '08 #1
5 1443
sicarie
4,677 Expert Mod 4TB
Aside of the fact that you never call oneDivide, I'd say that your compiler is probably doing an optimization on that - it takes an int and divides it by another int, and makes the result an int. Try using 1.0/n
Jan 28 '08 #2
Aside of the fact that you never call oneDivide, I'd say that your compiler is probably doing an optimization on that - it takes an int and divides it by another int, and makes the result an int. Try using 1.0/n
ah! ya I didnt really post the proper code. I wrote it quickly before leaving work

I tried this solution and it worked wonderfully! thank you !
Jan 28 '08 #3
sicarie
4,677 Expert Mod 4TB
No problem, feel free to post again if you run into something else.
Jan 28 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Better yet, is to not mix integers and floating point.

Either you will lose accuracy or suffer truncation and loss of data. Like when you try to convert a double to an int and the compiler won't do it.

Stick with all integer or all double.
Jan 28 '08 #5
hsn
237 100+
you have to assign n to a double value
double d=n;
thne do what ever you want to do.
Jan 29 '08 #6

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
14
by: Dave | last post by:
Hello all, Can anybody help with the problem below? I'm trying to define a conversion function that converts objects to function pointers and am getting a compile error on the line indicated...
1
by: QH Hong | last post by:
Hi, I have several classes and several members. class c1, c2, c3,... member of class c1 is m11, m12,... member of class c2 is m21, m22,... Each member takes one argument (double type) and...
6
by: Brian Ross | last post by:
Hi, I am trying to do something similar to the following: int Func1(int x, int y); double Func2(double x, double y); template <typename FuncT> // <- What would go here class CObjectT {
4
by: Tamer via DotNetMonster.com | last post by:
I'm developing a graphic engine. I got all equations made, I need only to understand how I can create a function that, once accepted three values (x; y; z), it returns the two values (X; Y) coming...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
20
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
13
by: RoughRyde | last post by:
Hi all, I'm totally new to C and hava a problem how to handle different types of variables. I have a function which which you call by: double calcdiff(const char *old, const char *new) I...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
8
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.