How to make exact decimal computations? - dart

I need to make decimal computations but sometimes the result is not exact.
0.009 + 0.001; // => 0.009999999999999998
How can I workaround that ?

You can use the decimal package. This package enables to make computations on decimal numbers without loosing precision like double operations.
Decimal.parse('0.2') + Decimal.parse('0.1'); // => 0.3
Decimal.parse('0.2') returns a new Decimal object that can be handled like num (by the way Decimal is not a num because num cannot be used as superclass or implemented).
To make your code shorter you can define a shortcut for Decimal.parse :
final d = Decimal.parse;
d('0.2') + d('0.1'); // => 0.3

Related

Is there a way to set a max limit to math operations in dart?

I have the following math operation:
var _baseFontSize = _userfontsize*8;
if (_baseFontSize > 14) { _baseFontSize = 14.0; }
Essentially whatever the _userfontsize is, my _baseFontSize should be 8x that number, but never exceeding 14.0.
Instead of doing this math operation in 2 lines as might be conventional, is there a way to set a max limit to this (or any given) math operation in dart?
You can import dart:math and do something like this with min:
var _baseFontSize = min(_userfontsize * 8, 14.0);
T min<T extends num>(T a, T b)
Returns the lesser of two numbers.

How can I get the expected output in Dart?

I am trying to get the output as below:
The problem is from URI-1098 Sequence IJ 4.
My Code is:
void main() {
double x = 0;
double y = 1;
for(double i = x; i<=2; i+= .2){
for(double j=y; j <= y + 2; j++){
if(i==0 || i==1 || i==2){
print('I=${i.toStringAsFixed(0)} J=${j.toStringAsFixed(0)}');
}else{
print('I=${i.toStringAsFixed(1)} J=${j.toStringAsFixed(1)}');
}
}
y += 0.2;
}
}
And my output is:
I=0 J=1
I=0 J=2
I=0 J=3
I=0.2 J=1.2
I=0.2 J=2.2
I=0.2 J=3.2
I=0.4 J=1.4
I=0.4 J=2.4
I=0.4 J=3.4
I=0.6 J=1.6
I=0.6 J=2.6
I=0.6 J=3.6
I=0.8 J=1.8
I=0.8 J=2.8
I=0.8 J=3.8
I=1 J=2
I=1 J=3
I=1 J=4
I=1.2 J=2.2
I=1.2 J=3.2
I=1.2 J=4.2
I=1.4 J=2.4
I=1.4 J=3.4
I=1.4 J=4.4
I=1.6 J=2.6
I=1.6 J=3.6
I=1.6 J=4.6
I=1.8 J=2.8
I=1.8 J=3.8
I=1.8 J=4.8
I=2.0 J=3.0
I=2.0 J=4.0
I=2.0 J=5.0
I am not getting desired outputs for the last 3 lines of the output. Can anyone kindly show the the error in my logic.
Thanking you all in advance.
You're using doubles.
That's the issue here. You are adding 0.2 repeatedly to a value starting at 0, but 0.2 is not precisely representable as a double. The 0.2 literal really represents the double value 0.200000000000000011102230246251565404236316680908203125 which is close to 0.2, but not exactly there.
The values you get by adding 0.2 to itself ten times loses some precision along the way. It hits 1.0 precisely, but then the next addition needs to drop some bits so the next value is actually below 1.2. The final result is 1.9999999999999997779553950749686919152736663818359375 which is not == 2.0, so your check for == 2.0 doesn't trigger. When you ask for the value with one decimal, it does round to "2.0", so you can't tell unless you print the actual value.
There are multiple ways to get around the problem.
One is to not use doubles. If you keep the numbers as integers by multiplying them by ten, then you just need a way to create the string to print. That could be
String toPrint(int value) {
var string = value.toString();
var lead = string.substring(0, string.length - 1);
if (string.endsWith('0')) return lead;
return "$lead.${string.substring(string.length - 1)}";
}
Alternatively, you can keep using doubles, but do .toStringAsFixed(1) and then check whether the last digit is zero. If it is, cut off the last two characters before printing.

Memory/CPU optimzation?

My program uses alot of memory and Processing power, I can only search up to 6000, is there any way to reduce the amount of memory this uses? This will really help with future programming endevours as it will be nice to know how to work with memory smartly.
ArrayList<Integer> factor = new ArrayList<Integer>();
ArrayList<Integer> non = new ArrayList<Integer>();
ArrayList<Integer> prime = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter how high we want to search");
long startTime = System.nanoTime();
int max = sc.nextInt();
int number = 2;
while (number < max)
{
for (int i=0;i<prime.size();i++)
{
int value = prime.get(i);
if (number % value == 0)
{
factor.add(value);
}
else
{
non.add(value);
}
}
if(factor.isEmpty())
{
prime.add(number);
}
else
{
composite.add(number);
}
factor.clear();
number++;
}
int howMany=prime.size();
System.out.printf("The are "+howMany+" prime numbers up to " +max + " and they are: " +prime );
System.out.println();
}
You do not say what language you are using, so this answer will be general.
To store primes up to 6,000 you only need about 3,000 bits which is less than 380 bytes. Your basic solution is the Sieve of Eratosthenes and the fact that 2 is the only even prime. You set up the sieve to handle only odd numbers, which halves the storage needed. Since the sieve only holds prime or not prime for each odd number, the storage can be reduced to a single bit for each number.
Once you have set up your sieve, there are many sites including this one which have instructions in different languages, you just need to retrieve the prime/not prime value from the sieve for the numbers in your range. Here is the pseudocode for checking if a number is prime, assuming the sieve has already been set up:
boolean function isPrime(number)
// Low numbers
if (number < 2)
return false
endif
// Even numbers
if (number is even)
return number == 2
endif
// Odd numbers >= 3
return sieve[(number - 1) / 2] == 1
end function
Low numbers are not prime. 2 is the only even prime; all other even numbers are not prime. The prime flag for the odd number 2n+1 is stored at bit n in the sieve. This assumes that the language you are using allows bit level access, something like a BitSet in Java.

How to make a value_of_things function f#

Hey guys let's say I have a function that gets that day's rate of how much something costs, by the pound, and multiplies it by how many pounds a customer wants. i.e
let convert_func (crab_rate, lobster_rate);
//and then on a certain day it is
let (crab_rate, lobster_rate) = convert_fun(3.4, 6.8); // $3.8 a pound for crab, $6.8 a pound for lobster.
// 10 being how many pounds i want.
crab_rate10 ;
Then my out put would be whatever 38 since ($3.8 * 10lbs) = $38
I tried doing if statements so that when the user just wants to find out the total cost of one thing and not both. But I keep getting errors. I can't figure out how to store the rate values in the parameters and then calling the function.
This is what i tried
let crab_rate (pound, rate) = (float pound) * rate;
let lobster_rate (pound, rate) = (float pound) * rate;
let convert_func (crab_rate, lobster_rate)= function (first,second ) ->
if crab_rate then (float pound) * rate;
elif lobster_rate (float pound) * rate;
let (crab_rate, lobster_rate) = convert_fun(3.4, 6.8); // $3.8 a pound for crab, $6.8 a pound for lobster.
// 10 being how many pounds i want.
crab_rate10 ;
I think you should start by making a general function for converting a cost/weight and a weight into a cost. In F#, you can even use units of measure to help you:
[<Measure>] type USD // Unit of measure: US Dollars
[<Measure>] type lb // Unit of measure: lbs
let priceForWeight rate (weight : float<lb>) : float<USD> =
rate * weight
The nice thing about functional languages with curried arguments is that we can easily use partial function application. That means when we have a function that has two arguments, we can choose to supply just one argument and get back a new function from that one remaining argument to the result.
We can therefore define a further pair of functions that use this priceForWeight function.
let crabPriceForWeight weight = priceForWeight 3.8<USD/lb> weight
let lobsterPriceForWeight weight = priceForWeight 6.8<USD/lb> weight
Notice that we've used our original function to define two new functions with fixed rates.
We can then evaluate it like this:
let crabPrice10 = crabPriceForWeight 10.0<lb> // result 38.0<USD>
Of course, you can also define a function that returns both prices together as a tuple for a supplied weight:
let crabAndLobsterPriceForWeight weight =
crabPriceForWeight weight, lobsterPriceForWeight weight

Floating point getting truncated in bison grammar

I finally got around to learning the basics of lex and bison. The problem I had was that I was calculating how much money I was going to give to my co-worker for picking up a burrito, and didn't like doing it manually.
For example, a $7.75 burrito + 20% tip can be figured out using 7.75*(1 + 20/100.0). However, I'd rather have the computer just take $7.75 + 20% and do it for me.
So I made this: https://github.com/tlehman/tipcalc
The lexing rules are
%%
\$ return TOKDOLLAR;
\% return TOKPERCENT;
[0-9]+(\.[0-9]+)* yylval=atof(yytext); return NUMBER;
[ \t]+ /* eat whitespace */
[\+\-] return TOKOP;
%%
And the parsing rules are
%%
start:
dollars TOKOP percentage
{
double dollars = $1;
double percentage = ($3)/(100.0);
double total = dollars + dollars*percentage;
printf("debug: dollars = %f\n", dollars);
printf("debug: percent = %f\n", percentage);
printf("%.2f", total);
}
dollars:
TOKDOLLAR NUMBER
{
$$ = (double)$2;
}
percentage:
NUMBER TOKPERCENT
{
$$ = (double)$1;
}
%%
The only problem is that dollars is getting handled incorrectly, when I run
$ echo '$7.75 + 20%' | ./tipcalc
I get this output:
debug: dollars = 7.000000
debug: percent = 0.200000
8.40
The dollars value is getting rounded somewhere. I think the rounding is happening after lexing since percentage seems to work with all the values I threw at it. I can't figure out where it is happening, have any ideas?
By default, the values passed around by the Bison-generated parser (yylval and the dollar things) are integers. So unless you explicitly tell Bison they are doubles, they will be integers. This includes yylval, so the truncation happens already here: yylval=atof(yytext);

Resources