Advertisement

05.12.2008 at 02:45AM PDT, ID: 23394007
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Floating Point Precision

Tags: Borland, C++ builder, 6.0
I am using C++ builder 6.0

I have 2 forms which are part of my program.

When the user presses button1 on form 1 the secord form is called using the code below:

form2 = new Tform2(this);
form2->ShowModal();
delete form2;

I have alot of variables that are declared as floats on the second form.  I carry out alot of calculations so sometimes precision is lost.
float precision = 0.001;

if (fabs(percentage) < precision)
{
         percentage = 0.00;
}

I have to cater for the fact that sometimes the value can be zero so I use the above code to give the value zero instead of -1.014E which I was sometimes getting.

I then use the code below

string convertToString(const float value)
{
  stringstream ss500;
  ss500 << fixed <<  setprecision(2) << value;
  return ss500.str();
}

to ensure all values have 2 decimal places.  

Most of the time everything works fine but I have noticed that sometimes when I close then open the form again values that should be 0.00 are sometimes 12345.56777777 (example) or shown as -0.00 instead of 0.00 is there anyway of ensuring this does not happen as it looks very messy on the screen.

Any help greatly appreciated
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: lisa_mc
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
Views: 4
Translate:
Loading Advertisement...
05.12.2008 at 02:51AM PDT, ID: 21545571

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 03:58AM PDT, ID: 21545810

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 05:38AM PDT, ID: 21546301

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 05:40AM PDT, ID: 21546321

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 06:13AM PDT, ID: 21546617

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 06:16AM PDT, ID: 21546647

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 06:41AM PDT, ID: 21546883

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 06:51AM PDT, ID: 21546959

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:02AM PDT, ID: 21547028

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:06AM PDT, ID: 21547068

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:21AM PDT, ID: 21547221

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:43AM PDT, ID: 21547402

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.14.2008 at 01:48AM PDT, ID: 21562161

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.14.2008 at 02:55AM PDT, ID: 21562463

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.14.2008 at 02:56AM PDT, ID: 21562466

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.14.2008 at 02:57AM PDT, ID: 21562472

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.14.2008 at 02:59AM PDT, ID: 21562479

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.12.2008 at 02:51AM PDT, ID: 21545571

Rank: Genius

>> sometimes 12345.56777777 (example)

Can you show actual values that you get ?


>> or shown as -0.00

if the value was set like this :

         percentage = 0.00;

that shouldn't happen. I assume there's some more code to this that you haven't shown.



In any case, if you want precise values and precise calculations, you shouldn't use floating point values. Rather go with fixed point values.
 
05.12.2008 at 03:58AM PDT, ID: 21545810

Rank: Sage

As an aside, but not a direct answer to your question...

If you're not used to doing floating point arithmetic in C/C++ you can get some nasty surprises. The binary representation of a floating point number is not precise and you can get unexpected results. You need to be aware of this as it means you have to be very careful when, for example, comparing 2 floating point numbers for equality. I'd recommend you read the following, this link to a Q&A about floating point numbers in C/C++...

http://www.codeguru.com/forum/printthread.php?t=323835

-Rx.
 
05.12.2008 at 05:38AM PDT, ID: 21546301
one of the values I was getting was 713148764706369830600000000.00 where it should have been 0.00
 
05.12.2008 at 05:40AM PDT, ID: 21546321

Rank: Genius

>> 713148764706369830600000000.00

How did you obtain that value ? What was the original data, and what were the calculations done on it ?
 
05.12.2008 at 06:13AM PDT, ID: 21546617
Original data is read in from a file into an array of structs and that value was then converted into a float value.  The value was then if matched read into another array of type float.  This value was then divided by 100 and reassigned to itself ie value = value/100 .

Then the fabs and stringstream as described earlier was done.
 
05.12.2008 at 06:16AM PDT, ID: 21546647

Rank: Genius

>> Original data

What was the original data ?


>> read in from a file

How was it read (code ?) ?


>> and that value was then converted into a float value.

Converted from what ? Converted how ? (code ?)


>> The value was then if matched

Matched how ? (code ?)

 
05.12.2008 at 06:41AM PDT, ID: 21546883
Read in expense from file

getline(infile2e1, array2e[i].expensee2, '\n') // to new line charater as this is last in record

Converts string to float

exp2e[i] = (float)atof(array2e[i].expensee2.c_str());

                for(int i=0; i<linecount2b; i++)    // number of records in meters file
                {
                        for(int j=0; j<linecount2e; j++)  // number of records in EXPT file
                        {
                                if(meterall2b[i] == expenseall2e[j]) // if a machine numbers matches another machine numer in the expense file
                                {                          
                             
                                        exp_count2e[i] = exp2e[j];
                                        flt_count2e[i] = flt2e[j];
                                        refill_count2e[i] = refill2e[j];

                                }
                                        // if no match is found
                                        // exp_count2e[i] , flt_count2e[i] and refill_count2e == 0
                        }
                }

        for(int i=0; i<linecount2b; i++)  // divides by 100 to give value in pounds
        {
             
                exp_count2e[i] = (exp_count2e[i]/100);

        }

Then fabs and stringstream is carried out
 
05.12.2008 at 06:51AM PDT, ID: 21546959

Rank: Genius

>> if(meterall2b[i] == expenseall2e[j])

Are you comparing floats here ? If so, that's not gonna work reliably !


>> exp2e[i] = (float)atof(array2e[i].expensee2.c_str());

What's the data read from the file ?
What's the type of exp2e[i] ?
Did you verify that the value in it is correct ?


>>                                         exp_count2e[i] = exp2e[j];

What's the type of exp_count2e[i] ?


>> Then fabs and stringstream is carried out

And what's the value that it showed as output ?
 
05.12.2008 at 07:02AM PDT, ID: 21547028
meterall and expenseall are strings

the value read from file will be 3000 or 0 or 1000 (value is shown as pence)

exp2e is a float

I dont know the value being read into it it could be any number as it is a pence value ie could be 0 could be 150 or so on.  I did test to make sure that the file was being read in correctly

exp_count2e is also a float

The value shown could be 0.00 then when I close the form and reload it - it gave me the value

713148764706369830600000000.00 this doesnt happen everytime just at random
 
05.12.2008 at 07:06AM PDT, ID: 21547068

Rank: Genius

>> the value read from file will be 3000 or 0 or 1000 (value is shown as pence)

Then why read it as a float ?


>> I dont know the value being read into it it could be any number as it is a pence value ie could be 0 could be 150 or so on.

Well, can you check which of those values caused an output of 713148764706369830600000000.00 ? Can you also check at what point in the code the value became wrong ?


>> then when I close the form and reload it - it gave me the value

Are you sure that closing the form doesn't invalidate the data ? ie. that the next time you show it, it shows a random value ?
 
05.12.2008 at 07:21AM PDT, ID: 21547221
I could read it as an int then try that

Im not sure if it is the form reloading because when I close the form I free all the memory used

Ill try this the other way ill get bk to you
 
05.12.2008 at 07:43AM PDT, ID: 21547402

Rank: Genius

>> because when I close the form I free all the memory used

Freeing the memory would invalidate it :) You can't use any data from a freed memory block, because you can't rely on its contents any more - they might have changed.
 
05.14.2008 at 01:48AM PDT, ID: 21562161
Sorry didnt get near a pc yesterday

I think it is something to do with opening and closing the forms because the first time the form is loaded everything is fine.

In my program all my information comes from text files so everytime the user selects a different menu option I have to read in the text file manipulate the data and display it to the user.  I cant read in the file once as the user can modify the information through certain menu options so I have to overwrite any changes.

Im the above example I read my files into array of structs using arrays of pointers

eg

double *array1;
array1 = new double[50];
then when I close the form I use
delete [] array1;

The reason why I use pointers is because I constantly need to update the information and using just arrays didnt work when I reloaded the form.

Even if there was a more practical way to read the information in and manupilate it without having to change code 2 much - I would def consider that - sorry C++ is not my main language im learning as I go so there probably are better ways of doing this.

Also I know the arrays are gettin incorrect information when I reload the form is there anything more I can do 2 make sure the data is wiped when I reload the form

 
05.14.2008 at 02:55AM PDT, ID: 21562463

Rank: Genius

As I said : if you delete the memory, then you invalidate the data ... in other words : if you want to keep the data, don't delete it :)
Accepted Solution
 
05.14.2008 at 02:56AM PDT, ID: 21562466
Hiya

I figured it out in the code below if meterall2b[i] == expenseall2e[j] then each array was assigned different values.  
If a match wasnt found then I assumed that the arrays would just be zero oh how wrong I was

I have changed the loop now so that if a match isnt found then value = 0.

Thanks for pointing me in the right direction infinity08 thats a major problem sorted
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
               for(int i=0; i<linecount2b; i++)    // number of records in meters file
                {
                        for(int j=0; j<linecount2e; j++)  // number of records in EXPT file
                        {
                                if(meterall2b[i] == expenseall2e[j]) // if a machine numbers matches another machine numer in the expense file
                                {                           
                              
                                        exp_count2e[i] = exp2e[j];
                                        flt_count2e[i] = flt2e[j];
                                        refill_count2e[i] = refill2e[j];
 
                                }
                                        // if no match is found 
                                        // exp_count2e[i] , flt_count2e[i] and refill_count2e == 0
                        }
                }
Open in New Window
 
05.14.2008 at 02:57AM PDT, ID: 21562472

Rank: Genius

>> I assumed that the arrays would just be zero oh how wrong I was

No ... You can't depend on that. It will be whatever happened to be in memory at that time.
 
05.14.2008 at 02:59AM PDT, ID: 21562479
I know that now i have learned my lesson thanks :-)
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628