picture format negative numbers - sas

I try to format the number into billions, millions and thousands as below
proc format;
picture bmk_fmt (round)
low - 0 = '000,000,000,000)' (prefix='($')
0 - 1e3 = '000,000,000,000' (prefix='$')
1e3 - 1e6 = '000,000,000,009K' (mult=1e-3 prefix='$')
1e6 - 1e9 = '000,000,000,009.9M' (mult=1e-5 prefix='$')
1e9 - high = '000,000,000,009.9B' (mult=1e-8 prefix='$');
run;
However, how can I extend such setting to negative numbers?
E.g (1.2M) & (353K)

You provide almost all information to solve the problem.
I've just have to use basic math.
Using a < sign you can exclude a value from a range e.g. 0 from a negative range.
Here is a final solution.
proc format;
picture bmk_fmt (round default=9)
low - -1e9 = '009.9B)' (mult=1e-8 prefix='($')
-1e9 <- -1e6 = '009.9M)' (mult=1e-5 prefix='($')
-1e6 <- -1e3 = '009.9K)' (mult=1e-2 prefix='($')
-1e3 <-< 0 = '009.9)' (prefix='($')
0 -< 1e3 = '009.9' (prefix='$')
1e3 -< 1e6 = '009.9K' (mult=1e-2 prefix='$')
1e6 -< 1e9 = '009.9M' (mult=1e-5 prefix='$')
1e9 - high = '009.9B' (mult=1e-8 prefix='$');
run;

Related

Convert decibel range to byte range

Greeting,
I'm trying to find a formula to convert a range where:
min = -100db and max = -30db
to:
min = 0 and max = 255
for example: -60db = ?
Seems easy but it makes my head spin.
Assuming you mean the result to stay in terms of db, you're asking for a simple linear interpolation:
f(x) = ymin + (x - xmin)*(ymax - ymin)/(xmax - xmin)
or in your case,
f(x) = 0 + (x + 100)*(255 - 0)/(-30 + 100)
f(-60 db) = 145.714
If instead you're talking about converting db to a scale factor with which to multiply an audio signal, then it's a bit more complex. For example, to multiply an audio signal by 0 is negative infinity db. So (at the very least) you'd have to special case that.

How do I convert this to precise columns and rows?

having a hard time converting to neat columns and rows. Can anyone help?
I have been trying to set columns and rows to 0 and work from there but I cant figure it out.
I just dont know where to insert the information.
n = 5
rate = 0.05
for n in range(0, 3):
principal = 10000
for n in range (0,6):
principal <= 15000
simple = principal * (1 + rate * n)
compound = principal * (1 + rate)**n
ratea = rate * 100
ratea = int(ratea)
principal = int(principal)
print((ratea),"% $",principal," $","{:.2f}".format(simple)," $","{:.2f}".format(compound))
principal = principal + 1000
rate = rate + 0.05
Use the string method format.

C++ Weird Variable Issues

I'm using the following code to calculate and display the final score for a math game in C++.
int score = (correctNumber / 3) * 100;
cout << score;
The variable "correctNumber" is always a value between 0 and 3. However, unless "correctNumber" = 3, then the variable "score" always equals "0". When "correctNumber" equals 3 then "score" equals 100.
Say "correctNumber" was equal to 2. Shouldn't "score" be 67 then? Is this some issue with int variable type being unable to calculate decimal points?
You are doing math as integer so 1 / 3 is 0.
Try:
int score = (100 * correctNumber) / 3
and if you want to round:
int score = (100 * correctNumber + 1) / 3
I'm assuming correctNumber is an int, based on what you described. What's happening is integer truncation. When you divide an int by an int, the result always rounds down:
1/3 = 0.3333 = 0 as an integer
2/3 = 0.6667 = 0 as an integer
3/3 = 1.0000 = 1 as an integer
The easy way to remedy this here is to multiply it first:
int score = correctNumber * 100 / 3;
However, this still leaves you with 66 for 2, not 67. A clear and simple way of dealing with that (and many other rounding situations, though the rounding style is unconfigurable) is std::round, included since C++11:
int score = std::round(currentNumber * 100. / 3);
In the example, the dot in 100. makes 100 a double (it's the same thing as 100.0), so the result of the operation will be the floating-point value you want, not a pre-truncated value passed in as a floating-point value. That means you'll end up with 66.66666... going into std::round instead of 66.
Your guess is correct. int can't store real numbers.
But you can multiply first, and then divide, like
score = correctNumber * 100 / 3;
score will have 0, 33, 66, 100, depending on values of correctNumber
The problem is that (correctNumber / 3) is an integer, so you can't get 0.666 or any fraction to multiply by 100, which what I believe is you want.
You could try to force it to be a float like this:
int score = ((float)correctNumber / 3) * 100;
This, however, will give you 66 instead of 67, cause it doesn't round up. You could use C99 round() for that.
int score = round(((float)correctNumber / 3) * 100);
UPDATE:
You can also use + 0.5 to round, like this:
int score = (correctNumber / 3.0f) * 100.0f + 0.5f;

cpp - std average

Is there the better way to calculate the average of two doubles? How could I improve / correct my code below?
double original_one, original_two; // can be any double >= 0
double used_one = original_one;
double used_two = original_two;
if ( original_one == 0 ) used_one = 1;
if ( original_two == 0 ) used_two = 1;
double average = used_one * used_two / 2; // average!
The arithmetic mean of two numbers is computed by adding them, and dividing by two...
double average = (original_one + original_two) / 2;
This is one way to compute the average, there are several more but this is the most common.

Calculating proportion with negative float values

I'd like to know if there's any way in C++ to calculate a proportion involving possibily negative values in both vars and extremes.
My goal is to sync a float text input widget with fixed extremes ( eg the user can input any double value between A (min) and B (max) with A,B=any_constant_real_number ) with a slider who can only slide between 0 and 100 ( to simplify ).
If A and B are positive everything is trivial. as
val_slider = ((val_textin-A)*100)/(B-A)
but as A and B can be assumed real it looks to me the only possibility is to use several if/cases, or huge formulas involving a lot of abs() and checks over 0-divisions, whose are quite error prone and very cost intense compared to such an easy task.
Is there any faster and shorter way to achieve the same in c/c++/stl?
Pardon my bad english. Any hint? Thank you.
Your formula should work fine with negative values of A and B as well, as log as A < B.
Example, if you want the user to be able to enter values from -100 to 100, and map these to a slider which goes from 0 - 100, when the user enters -90 you get:
((-90 - A) * 100) / (B - A) = ((-90 - (-100)) * 100) / (100 - (-100))
= 10 * 100 / 200
= 5
An input value of 50 results in a slider value of:
((50 - A) * 100) / (B - A) = ((50 - (-100)) * 100) / (100 - (-100))
= 150 * 100 / 200
= 75
I don't know C++, but I do know Math, so try:
val_slider = 100 * ( val_textin - A ) / ( B - A )
Hey wait. That's exactly what you have. Test case..
A=-200, B=+200, val_texin = 100 (75% of bar, right?)
val_slider = 100 * ( 100 - -200 ) / ( 200 - - 200 )
= ( 300 ) / ( 400 ) * 100
= 75
See, you got it right. The only thing that COULD happen is B==A, but that can't be accounted for with math and requires a single IF. If they are equal, val_slider is exactly B (or A, as they are equal).