Hot Topic (More than 10 Replies) Doing research for Ray (Read 3817 times)
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Doing research for Ray
Dec 9th, 2005 at 12:47am
Print Post Print Post  
I spend most of today doing research for Ray. He has decided ("decided" being a nice way to say that he was ordered to by the people that pay him) to take on the problems associated with floating point numbers and their inherit imprecision.

in C++:
Code
Select All
double aa;

  aa = 10.5;
  printf("%.14f", aa) // print with 14 places to the right of the decimal point
 



will print the variable "aa" with a good deal of precision. On an Intel chip it will print: "10.49999999999999". This is because computers can only store, with absolute precision, fractions that can expressed as a power of two. All of the others are an approximation. So, 1/4th (0.25) can be stored absolutely correctly, but 1/10th (0.1) cannot. Usually this does not matter at all. So what if the number is off by the width of a hair on the scale of the width of the galaxy? But when one is trying to round that number at the second decimal place, that imprecision gets shifted into the integer portion of the floating point and becomes "1049" instead of the desired "1050".

So while Ray was trying out everything he could think of, I was on the internet looking for white papers and published techniques. I finally found one that looked pretty good. Ray is at home right now, testing it with, as he puts it, "all the numbers". I hope it works out. The last technique he tried worked well until the number got in the hundreds of billions. Then it got funny.
  

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 #1 - Dec 9th, 2005 at 3:21pm
Print Post Print Post  
Well. The @Rnd and @Decimals that I went home with last night proved to be well better in some cases but worse in most cases that I found. So I re-wrote @Rnd a few more times last night and finally finished one early this morning that appears to be accurate up to tens of millions where it develops a slight bit of inaccuracy on some numbers. For Example 123456888.987 would round to 123456888.99000001

So after a few hours of sleep, with visions of floating point numbers dancing in my head, I woke up and re-wrote @Decimals and tested it. So far so good.

Soon I have to do a speed test to see how much time the increased accuracy is going to cost me.

and later today I'm going to the airport to pick up Erika.

-Ray
  

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


I came, I saw, I'm still
trying to figure it out!

Posts: 208
Location: Green Bay, WI
Joined: Dec 17th, 2003
Re: Doing research for Ray
Reply #2 - Dec 9th, 2005 at 4:55pm
Print Post Print Post  
"..and later today I'm going to the airport to pick up Erika."

What's your pick up line??  Roll Eyes
  
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 #3 - Dec 9th, 2005 at 5:25pm
Print Post Print Post  
A pickup line? hmm well since it is mighty cold in cleveland how about.....

"My car has heat."

yeah, maybe if she wasn't my Aunt.

-Ray
  

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



Posts: 2530
Joined: Nov 22nd, 2002
Re: Doing research for Ray
Reply #4 - Dec 10th, 2005 at 12:07am
Print Post Print Post  
Today the research continued. Most platforms (including Linux) come with a round() function as part of the C libraries. Like many other standard functions, MS somehow managed to miss that one. When Ray saw that Linux supported a "built-in" round() function but MS did not, he declared, "Okay, everyone switch!"

I guess that one of the reasons one shouldn't use development tools from a company that also provides competing applications.

I found a source example from MS, using a lookup table instead of the pow() function. But, Ray tried it and found it less accurate than several of the others he has tried.

We sent the best of them out to the alpha testers to see what they think of it. Hopefully, it'll be okay.

I started work on getting the generic table to know how to print itself.

I also did some more investigation on how to use bidirectional pipes to use standard I/O to communicate with other applications. All's well on Linux, but MS does not support process forks or standard I/O (in windows programs) - both of which are critical. I may be able to replace the fork with threads, but the standard I/O will probably bring up the horrible "black window" that @Shell causes (for the same reason).

Erika came back from teaching classes in Dallas today. Welcome home Erika!
  

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


your worst nightmare

Posts: 2
Joined: Nov 16th, 2003
Re: Doing research for Ray
Reply #5 - Dec 11th, 2005 at 2:31am
Print Post Print Post  
bet I can pick her up with a better line  " Honey, here's my paycheck"
  

if you go to bed with the government, be prepared for more than a good night's sleep
Back to top
 
IP Logged
 
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #6 - Dec 13th, 2005 at 1:51pm
Print Post Print Post  
http://www.karenware.com/powertools/ptcalc.asp

The above downloadable calculator is fully addressing the issue, may be, as for me, it would make sense to ask Karen how she managed the problem under VB6 ... (to get a starting point ...), VB6 source code download link:

http://www.karenware.com/progs/ptcalc-src.exe
  
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Doing research for Ray
Reply #7 - Dec 13th, 2005 at 1:59pm
Print Post Print Post  
http://www.moffsoft.com/

On that note, this company offers a similar program.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #8 - Dec 13th, 2005 at 2:03pm
Print Post Print Post  
Quote:
http://www.moffsoft.com/

On that note, this company offers a similar program.


Citing ex karenware.com 's calculator description:

quote
Karen's Calculator is a high-precision calculator. Unlike ordinary calculators, it returns completely accurate results, even with operands containing thousands of digits. If you're patient, operands and results containing hundreds of thousands of digits are possble too!

Most calculators are only accurate when dealing with numbers containing a few digits -- usually 16 to 32. When asked to calculate using larger numbers, they either fail (displaying an error) or return results that are only approximately accurate.

Karen's Calculator can easily handle numbers and results containing many thousands of digits with complete accuracy. It also includes an on-screen "tape" -- which can be saved to disk -- versatile memory features, and more!
unquote

To say, MOFFSOFT's product is certainly NOT comparable...
  
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Doing research for Ray
Reply #9 - Dec 13th, 2005 at 2:19pm
Print Post Print Post  
Calm down sir...
Sir, please have a seat...
Sir, return to your seat or I will have to get the air marshall involved... Grin Grin Grin

Sorry, I couldn't resist. Wink
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #10 - Dec 13th, 2005 at 2:25pm
Print Post Print Post  
http://www.gbcsci.com/products/uv/whats_new.asp

Rather than to look into Karen's VB6 source code to get some techniques, drill down to:

quote
SBasic Programming Language for UV-Visible Software
Simplify routine analyses and complex data manipulation with SBasic macros. A newly published application note from GBC Scientific Equipment describes how the SBasic language is used with GBC's Spectral software to automate data acquisition and manipulation for UV-Visible analysis.

To illustrate the structure of an SBasic program and the associated commands, a macro for the determination of Chlorophyll is discussed. This Chlorophyll analysis macro automates the calculations required for Chlorophyll determinations.

A step by step description of the Chlorophyll Macro code is described. This macro can also be used as a model and modified for any other analysis.

For your convenience this macro can be downloaded by clicking here (http://www.gbcsci.com/downloads/software/uvmacro1.exe).

For further details on this Application, the SBasic language or Spectral software, see your local GBC distributor or contact us (http://www.gbcsci.com/contact/index.asp).
unquote

  
Back to top
 
IP Logged
 
Masterank
Member
*
Offline



Posts: 15
Joined: Jan 21st, 2004
Re: Doing research for Ray
Reply #11 - Dec 13th, 2005 at 2:27pm
Print Post Print Post  
Quote:
Calm down sir...
Sir, please have a seat...
Sir, return to your seat or I will have to get the air marshall involved... Grin Grin Grin

Sorry, I couldn't resist. Wink


No need, already airborne into other numeric galaxies ...
8)
  
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 #12 - Dec 13th, 2005 at 4:32pm
Print Post Print Post  
Quote:
http://www.gbcsci.com/products/uv/whats_new.asp

Rather than to look into Karen's VB6 source code to get some techniques, drill down to:

quote
SBasic Programming Language for UV-Visible Software
Simplify routine analyses and complex data manipulation with SBasic macros. A newly published application note from GBC Scientific Equipment describes how the SBasic language is used with GBC's Spectral software to automate data acquisition and manipulation for UV-Visible analysis.

To illustrate the structure of an SBasic program and the associated commands, a macro for the determination of Chlorophyll is discussed. This Chlorophyll analysis macro automates the calculations required for Chlorophyll determinations.

A step by step description of the Chlorophyll Macro code is described. This macro can also be used as a model and modified for any other analysis.

For your convenience this macro can be downloaded by clicking here (http://www.gbcsci.com/downloads/software/uvmacro1.exe).

For further details on this Application, the SBasic language or Spectral software, see your local GBC distributor or contact us (http://www.gbcsci.com/contact/index.asp).
unquote




The SBasic that they are referring to is not Sesame Basic which is the SBasic that Sesame uses. The Sbasic that Sesame uses was written for Sesame by Andreas Goebel. I am going to take a guess and say that the SBasic they are referring to is Spectral Basic.
  

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 #13 - Dec 13th, 2005 at 8:02pm
Print Post Print Post  
Quote:
The SBasic that they are referring to is not Sesame Basic which is the SBasic that Sesame uses. The Sbasic that Sesame uses was written for Sesame by Andreas Goebel. I am going to take a guess and say that the SBasic they are referring to is Spectral Basic.


All well noted, it is my understanding that "The Cow"'s thread is looking for TECHNIQUES..., I further understand that (programming) languages may differ, but the TECHNIQUES to address to a certain issue remain the same. My posting was just meant to provide some insight into techniques used by others ...
  
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 #14 - Dec 13th, 2005 at 10:03pm
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.

An Example of this is below.

Code
Select All
double MyRound(double Value, int NumPlaces)
{
   double Factor;
   long Temp;

   Factor = pow(10.0, NumPlaces);
   Temp = (Value * Factor) + 0.5;
   return Temp / Factor;
} 



Using the above code 8.745 rounds to 8.74, instead of the expected(based on what my math teacher in 2nd grade said) 8.75. Why? Well because 8.745 is passed in as 8.744999999999999200 and multiplying by 100 gives you 874.499999999999890000 which if you add .5 gives you 874.999999999999890000 which when cast to a long gives you 874. divide by 100 and you get 8.74

Now that is just a simple rounding function, and may be considered bad coding by some, but it displays the problem that I am trying to work around.

The Calculator that you referenced earlier does not use doubles, as far as I can tell, to do it's math as doubles are limited to numbers between 2.2-308 and 1.8e308. Since she can do numbers that contain hundreds of thousands of digits that rules out the use of doubles. but on a side note thank you for posting that as it may be useful to me personally.

-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 #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