LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2015, 06:31 AM   #16
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197

I have a cute suggestion on deciding whether the two solutions are the same or different in a less rigorous but much more user friendly manner:

Select one of the simple means of converting the floating point to string (I think using a string stream is the best of those). Convert both solutions to string and compare the two strings. If the strings differ then from the user's point of view the numbers differ. If the strings are the same, then the user would consider the program flawed for thinking the numbers differ.

Having converted those numbers to strings once, do not convert them again by printing the numbers in the final output. Instead print those strings. Avoid errors by avoiding doing the same job twice, and especially avoid depending on the same job twice producing the same result. Floating point in optimized code often produces different results when the same computation appears in two different sections of code.
 
Old 02-17-2015, 10:21 AM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Not sure if floating point suffers the same things, but for integers, some issues you have to watch out for are the types of operations you are performing. Because when you divide or multiply this causes shifting and therefore loss of precision. For instance if you divide by 32 you shift the variable to the right by 5 bits and then lose whatever information was in bits 0-4 and you can never get it back.

I know that we have a conversion algorithm where we represent it as integers, scale the numbers up by about a factor of 4096 and when we perform multiplications and divisions of them, get our interim results, we end it by dividing back by 4096 to so that the placement of the least significant digit was retained. We then convert to floating point.

As far as another question you asked where the prof discussed solve_linear() and solve_quadratic(), they are suggesting that you use functions because it separates functional code for the benefits of readability and modularity in your programming. This way you can use the general functions solve_linear() and solve_quadratic() many times over without having to rewrite the code.
 
Old 02-17-2015, 10:33 AM   #18
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by rtmistler View Post
Not sure if floating point suffers the same things, but for integers, some issues you have to watch out for are the types of operations you are performing. Because when you divide or multiply this causes shifting and therefore loss of precision. For instance if you divide by 32 you shift the variable to the right by 5 bits and then lose whatever information was in bits 0-4 and you can never get it back.
Floating point is a log base 2 format. Loss of precision depends on the size of the number... and some things just don't work - such as adding 1.0 to a very large number. As the value gets larger, there become larger and larger gaps between the values - which is where roundoff causes problems, adding one to a value SHOULD increase it - but if the increased value is inside one of the gaps, it can get truncated.
Quote:
I know that we have a conversion algorithm where we represent it as integers, scale the numbers up by about a factor of 4096 and when we perform multiplications and divisions of them, get our interim results, we end it by dividing back by 4096 to so that the placement of the least significant digit was retained. We then convert to floating point.
Sometimes that conversion becomes "inexact" It depends on the magnitude as to how much of a problem it is.

http://en.wikipedia.org/wiki/Floatin...n_and_rounding

Last edited by jpollard; 02-17-2015 at 10:37 AM.
 
Old 02-17-2015, 11:06 AM   #19
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by rtmistler View Post
Not sure if floating point suffers the same things, but for integers, some issues you have to watch out for are the types of operations you are performing. Because when you divide or multiply this causes shifting and therefore loss of precision. For instance if you divide by 32 you shift the variable to the right by 5 bits and then lose whatever information was in bits 0-4 and you can never get it back.
You don't have to worry about "losing" anything when multiplying or dividing a float, you only have to worry about it when you're adding/subtracting, as jpollard mentioned.

eg 1: ((.001 * 1e10) / 1e10) will still be .001
eg 2: ((.001 + 1e10) - 1e10) will not be .001

Last edited by suicidaleggroll; 02-17-2015 at 11:08 AM.
 
Old 02-17-2015, 05:53 PM   #20
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by suicidaleggroll View Post
You don't have to worry about "losing" anything when multiplying or dividing a float, you only have to worry about it when you're adding/subtracting, as jpollard mentioned.

eg 1: ((.001 * 1e10) / 1e10) will still be .001
eg 2: ((.001 + 1e10) - 1e10) will not be .001
Depends on the optimization... The compiler can eliminate the add/subtract/multiply/divide of 1e10.

But hiding the values in different variables (and passing them via function calls) should defeat the compiler optimizations.

At one time, there was an IBM paper that analyzed different CPU implementations of floating point (and unfortunately, I can't find it now), and the errors that could accumulate. Several of the plots were for computing a spiral via successive arithmetic (I think it was a geometric spiral) where the exact same program was used on different computers from different manufacturers. Each one had the same spiral in the beginning - but after a certain number of iterations each computer would go off the deep end at different iterations, and in different ways. I believe all were using IEEE floating point - the errors would be in different optimizations and other individual differences of implementation.
 
Old 02-17-2015, 06:03 PM   #21
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by jpollard View Post
Depends on the optimization... The compiler can eliminate the add/subtract/multiply/divide of 1e10.
Yes of course. I meant the mathematical operations, not what the compiler decided should actually be executed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me to solve this problem mandyakishor Linux - Desktop 2 09-26-2011 10:57 AM
quadratic equation for python NEW JAPANBOY Programming 3 06-26-2011 11:59 PM
fomula for coefficient for quadratic equation JAPANBOY Programming 4 06-25-2011 01:21 PM
how to solve solve broken shell problem prasanth.george Red Hat 1 01-21-2011 09:48 AM
how to solve equation from string in java? rabbit2345 Programming 27 11-04-2008 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration