ijjo
ijjo

Reputation: 525

How to reduce large numbers to smallest values keeping relative size differences in C#

Here's my problem. I have potentially large numbers, anywhere from one hundred to one hundred million or even more. I want to feed these numbers to a chart plotter (http://benpickles.github.com/peity/ if you're curious). the problem is, when you have very large numbers the line chart ends up looking like a flat line since the numbers are so large the differences do not show up on such a small chart. But if you chart numbers like 2,5,8,10,15. you can easily see the chart line going up and to the right steeply.

So in order to plot my mini charts in a way that actually is meaningful, I need to reduce these large numbers into as small numbers as possible BUT maintain a relative difference between the numbers large enough that they plot on a chart well, like with the peity charts. I don't really need a perfect formula (not sure if one exists). If my "large" numbers are growing, I simply want single digit numbers that grow at the same relative pace as the large numbers. If they are some what flat, I want the chart to look flat, etc.

I don't have a strong math background so I don't know if there's an actual math term for this??

Upvotes: 2

Views: 8913

Answers (4)

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 62002

Suppose you have the following barometric pressures:

double[] p = { 101325.0, 101380.0, 101510.0, 101580.0, 101470.0, 101295.0, 100985.0, };

(numbers in pascals). If you plot them, the graph may look very straight and horizontal because the numbers have all very much the same magnitude.

To better see the changes, simply subtract some fixed number from every term. E.g.

double offset = 100000.0;
var pOffset = p.Select(x => x - offset).ToArray();

Then plot pOffset instead.

In the above example, I just picked the offset 100000.0 "magically". But you could use the minimum of all the values as an offset, that is

double offset = p.Min();

My code examples require using System.Linq;.

Upvotes: 1

Krzysztof Morcinek
Krzysztof Morcinek

Reputation: 1021

Look at the function Math.Log(...). I suppose you know it but in case http://en.wikipedia.org/wiki/Logarithm. Logarithm will basically flatter your graph.

Do it for every point before plotting. You can choose by experiments (if its visible enough) the logarithm base. From your description I suppose you should use some big number like 10.

newPlotValue = Math.Log(plotValue, 10); // or Math.Log10(plotValue);

Upvotes: 2

Mathias
Mathias

Reputation: 15391

From what I understand in your question, your issue is that the numbers you are plotting have very different scale. If that's the case, you may consider plotting them on a log/log scale: http://en.m.wikipedia.org/wiki/Logarithmic_scale

Upvotes: 2

nunespascal
nunespascal

Reputation: 17724

You only need to decide where is your plot scale.

You could take the smallest as the start of your scale.

The next is to decide a common factor, and show that in your units.

Like if numbers are greater than 1000, divide them all by 1000 and in your label show the scale as (in 1000s). Same can be used for in millions or in billions.

Upvotes: 0

Related Questions