Page Index Toggle Pages: 1 [2]  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) Doing research for Ray (Read 3813 times)
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #15 - Dec 14th, 2005 at 4:06am
Print Post Print Post  
...Thank you for that. What he is really looking for, for me, is a C/C++ solution to rounding floating point numbers(doubles) to a specific number of decimal places with the most accuracy that can be had...


Ray,

Fast Rounding of Floating Point Numbers in C/C++ on Wintel Platform
- http://ldesoras.free.fr/doc/articles/rounding_en.pdf -

and ROUND 4 C++ example ex http://cis.stvincent.edu/swd/basic/arithmetic.html

quote

/* Given:   Value      a floating point number
           NumPlaces  a positive integer giving the number of
                      decimal places to which to round the answer.
   Task:    To round Value to NumPlaces decimal places.  If NumPlaces
           is not postive, Value is returned unchanged as the answer.
   Return:  The rounded number in the function name.
*/   
float Round4(float Value, int NumPlaces)
   {
   int k, Temp;
   float Factor;
   
   Factor = 1;
   for (k = 0; k < NumPlaces; k++)
     Factor = Factor * 10;
     
   Temp = Value * Factor + 0.5;
   return Temp / Factor;
   }

unquote

Hope, it helps.

- M.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Doing research for Ray
Reply #16 - Dec 14th, 2005 at 3:34pm
Print Post Print Post  
The routine above is pretty much the standard way of rounding in C. It is nearly identical to the code that is in @Round right now - and is the starting point for Ray's exploration.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #17 - Dec 14th, 2005 at 8:58pm
Print Post Print Post  
Quote:
The routine above is pretty much the standard way of rounding in C. It is nearly identical to the code that is in @Round right now - and is the starting point for Ray's exploration.


Except, this is using FOR to loop while POW is a function ..., I understand from your previously mentioned:

quote
double MyRound(double Value, int NumPlaces)
{
   double Factor;
   long Temp;
   
   Factor = pow(10.0, NumPlaces);
   Temp = (Value * Factor) + 0.5;
   return Temp / Factor;
}
unquote

that it is talking LONG TEMP while INT TEMP is different ...

- M.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Doing research for Ray
Reply #18 - Dec 14th, 2005 at 9:03pm
Print Post Print Post  
Actually, Ray posted that example using "pow()".

The current code for @round is using almost precisely the code you posted, as it was authored by Andreas. In either case, whether looping by the number of decimal places, or using the pow() function (as in Ray's example), the result is the extremely similar. Just two different meanss to get to the same place (10 to the pwer of the number of decimal places required).

On Win32, "int" automatically declares a "long int" (32 bits).
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Doing research for Ray
Reply #19 - Dec 14th, 2005 at 9:11pm
Print Post Print Post  
The Pow version was one of the many I tried and I figured it would be the easiest to understand which is why I posted it.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #20 - Dec 14th, 2005 at 9:13pm
Print Post Print Post  
Quote:
The Pow version was one of the many I tried and I figured it would be the easiest to understand which is why I posted it.

-Ray


Ray,

read para 2.3 "Why it fails" and subsequent lines (ACCURATE METHOD) out of:

Fast Rounding of Floating Point Numbers in C/C++ on Wintel Platform
- http://ldesoras.free.fr/doc/articles/rounding_en.pdf -

- M.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Doing research for Ray
Reply #21 - Dec 14th, 2005 at 11:51pm
Print Post Print Post  
Both of us have seen it, and the examples have been compiled and tried.

The problem is not one of unknown technique. The techniques are generally known and implemented. By now, Ray has gathered all of the reliable methods. All of them have specific pros and cons within the context of the application. We are in the process of weighing those pros and cons.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #22 - Dec 15th, 2005 at 10:43am
Print Post Print Post  
Quote:
Both of us have seen it, and the examples have been compiled and tried.

The problem is not one of unknown technique. The techniques are generally known and implemented. By now, Ray has gathered all of the reliable methods. All of them have specific pros and cons within the context of the application. We are in the process of weighing those pros and cons.



Mark,

all well noted, good luck on the "floating point" and "rounding" mission, reading this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/ht...

I understood that the compiler must be as well capable to correctly "translate" the coding ...

- M.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Doing research for Ray
Reply #23 - Dec 15th, 2005 at 1:33pm
Print Post Print Post  
To a large degree, I think we may be dealing as much with Ray's admirable desire for mathmatical absolutes as with the numbers themselves.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Doing research for Ray
Reply #24 - Dec 15th, 2005 at 4:17pm
Print Post Print Post  
Quote:
we may be dealing as much with Ray's admirable desire for mathmatical absolutes
Maybe Ray can wrassle with this for a while:

It is a mathematics rule that what you do to one side of an equation must be done to the other side to keep them equal.
---------------------------------- Quote:
Start with the given:
m = n

Now do processing to the equation
-------------------------------------
Multiply both sides by "m":
m2 = mn
-------------------------------------
Subtract "n2" from both sides:
m2 - n2  = mn - n2   
-------------------------------------
Now factor both sides:
(m + n) ( m - n) = n(m - n)
-------------------------------------
Divide both sides by the common factor:
(m + n) = n
-------------------------------------
Substitute "m" for "n" (from given at start):
m + m = m
-------------------------------------
Combine terms:
2m = m
-------------------------------------
Divide both sides by the common factor:
2 = 1
-------------------------------------

Now that we have proven that 2 = 1,(Hmmmm, how can this be?  we did the same to both sides of the equation), let's see how many decimals of accuracy Ray can provide after rounding the result.
« Last Edit: Dec 16th, 2005 at 3:15am by Bob_Hansen »  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send Topic Send Topic Print Print