473,399 Members | 3,401 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,399 software developers and data experts.

Integers vs floats

12
Can someone explain the output I'm getting from the following 2 code portions? In the first, I've used n and sizeOfArrays as long doubles, and in the second I've made sizeOfArrays an integer, as this is the form needed for setting the number of elements in an array. However, the moment it's in integer form, the output is "wrong" ie not what I want it to be. Why is this? How can I get around it?

Expand|Select|Wrap|Line Numbers
  1.     long double h = 0.1;
  2.     long double n = 1.5/h;
  3.     long double sizeOfArrays = n + 1;
  4.  
  5.     cout.setf(ios::fixed);
  6.     cout.precision(7);
  7.  
  8.     cout << n << " and  " << sizeOfArrays << endl;
  9.  
Output is 15.0000000 and 16.0000000

Expand|Select|Wrap|Line Numbers
  1.     long double h = 0.1;
  2.     long double n = 1.5/h;
  3.     int sizeOfArrays = n + 1;
  4.  
  5.     cout.setf(ios::fixed);
  6.     cout.precision(7);
  7.  
  8.     cout << n << " and  " << sizeOfArrays << endl;
  9.  
Output is 15.0000000 and 15

If I set n as an integer, it gives an output value of 14, too. This I could understand better, and setting the array size as "n+1" naturally wouldn't work while n was a float, but I thought I could get around it all by creating a new variable called sizeOfArrays set as an integer. The results are above.
May 9 '07 #1
6 2227
BevG
12
To explain more clearly what output I was expecting: n should be = 15, doing the calculation manually, therefore "sizeOfArray" should be = 16. Calling n an integer makes its value = 14, and calling "sizeOfArray" and integer makes it 15. This is what I don't understand. I have to make one or the other an integer to be able to use it to describe the number of elements in the array.
The reason I want to do this all in code (when I know what the answers are!) is that I want to be able to change the value of h at will, and n (and therefore the desired array size) depends on the value of h, so doing it in code stops me from having to go all the way through the rest of the program changing every bit that's dependent on h.
May 9 '07 #2
JosAH
11,448 Expert 8TB
Read this article. If you've read it read it again and again.

kind regards,

Jos
May 9 '07 #3
AdrianH
1,251 Expert 1GB
Can someone explain the output I'm getting from the following 2 code portions? In the first, I've used n and sizeOfArrays as long doubles, and in the second I've made sizeOfArrays an integer, as this is the form needed for setting the number of elements in an array. However, the moment it's in integer form, the output is "wrong" ie not what I want it to be. Why is this? How can I get around it?

Expand|Select|Wrap|Line Numbers
  1.     long double h = 0.1;
  2.     long double n = 1.5/h;
  3.     long double sizeOfArrays = n + 1;
  4.  
  5.     cout.setf(ios::fixed);
  6.     cout.precision(7);
  7.  
  8.     cout << n << " and  " << sizeOfArrays << endl;
  9.  
Output is 15.0000000 and 16.0000000

Expand|Select|Wrap|Line Numbers
  1.     long double h = 0.1;
  2.     long double n = 1.5/h;
  3.     int sizeOfArrays = n + 1;
  4.  
  5.     cout.setf(ios::fixed);
  6.     cout.precision(7);
  7.  
  8.     cout << n << " and  " << sizeOfArrays << endl;
  9.  
Output is 15.0000000 and 15

If I set n as an integer, it gives an output value of 14, too. This I could understand better, and setting the array size as "n+1" naturally wouldn't work while n was a float, but I thought I could get around it all by creating a new variable called sizeOfArrays set as an integer. The results are above.
What you are experiencing is rounding error. When converting from a floating point number to a integral number, it takes the binary value of the floating point's integral part and truncates the rest.

A number like 14.9999999999999999999999999999999999999999999 will show up as 15 in the code you provided, but it is not actually 15. (If the 9 was repeated indefinitely, then it would actually be equal to 15. There is a proof, but I'm going leave that up to you as an exercise). So the truncation results in 14. This is an unfortunate side-effect of representing infinite floating point numbers using finite ones.

A possible work around would be to add a very small number to the value. Say 1e-8 (I picked that somewhat arbitrarily given you are printing to 7 digits). Or a more precise way would be to add epsilon<long double>() to it.

BTW, what is your reason for using long doubles? I do not see any need for it for the precision you require.


Adrian
May 9 '07 #4
BevG
12
Thanks, Jos, I'll read through that.

Adrian, the code is a portion of a program for numerical methods, so basically I'm just using long doubles because it's the highest accuracy I could use. Perhaps doubles would in fact suffice. But I stopped using regular floats very early on in these types of programs, presumably because I found the accuracy insufficient at the time, and I can't remember now whether I found the same problem with doubles.
Thanks for your reply, too - I'll see what I can do based on that. Is there no way just to round off instead of truncate?
May 9 '07 #5
BevG
12
All right, adding 1e-8 did the trick. Thanks!
May 9 '07 #6
AdrianH
1,251 Expert 1GB
Thanks, Jos, I'll read through that.

Adrian, the code is a portion of a program for numerical methods, so basically I'm just using long doubles because it's the highest accuracy I could use. Perhaps doubles would in fact suffice. But I stopped using regular floats very early on in these types of programs, presumably because I found the accuracy insufficient at the time, and I can't remember now whether I found the same problem with doubles.
Thanks for your reply, too - I'll see what I can do based on that. Is there no way just to round off instead of truncate?
Yeah, try using the round function ;)

See http://www.codecogs.com/reference/ma...hp?alias=round.


Adrian
May 9 '07 #7

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

Similar topics

5
by: Jørgen Cederberg | last post by:
Hi, using Python 2.2.1 on Windows and QNX I'm having trouble understandig why int() raises a ValueError exception upon converting strings. >>> int('-10') -10 >>> int('10') 10 >>> int(10.1)
22
by: Paul Prescod | last post by:
I think that in this case, Python is demonstrably better than Prothon. C:\temp\prothon\Prothon>python ActivePython 2.3.2 Build 232 (ActiveState Corp.) based on Python 2.3.2 (#49, Nov 13 2003,...
4
by: Timothy Fitz | last post by:
Why are all numberical literals in exponential notation floats? To me it is counter-intuitive for 1e3 to behave so fundamentally different from 1000. Timothy Fitz
14
by: Michael Brennan | last post by:
Hi, I wonder if there is any good reason to let different systems have different sizes of short, int and long? I think that makes it harder to plan which type to use for your program. For...
1
by: freedsbi | last post by:
i need to copy N unsigned short number that live in an N-dimensional array onto an equal size array of floats (the original array - unsigned short - is returned by the cfitsio library). can i use...
11
by: redefined.horizons | last post by:
I'm still pretty new to Python. I'm writing a function that accepts thre integers as arguments. I need to divide the first integer by te second integer, and get a float as a result. I don't want...
6
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5,...
16
by: luca bertini | last post by:
Hi, i have strings which look like money values (ie 34.45) is there a way to convert them into float variables? everytime i try I get this error: "numb = float(my_line) ValueError: empty string...
9
by: Mark Morss | last post by:
I would like to construct a class that includes both the integers and None. I desire that if x and y are elements of this class, and both are integers, then arithmetic operations between them,...
42
by: lorlarz | last post by:
Contrary to what one authority in the JavaScript field says: JavaScript does make errors when dealing with just with integers. This authority (Douglas Crockford.) says: "integer arithmetic in...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.