Kotlin , code enters if statemant even when condition is false - android

I'm really confused here ... looks like a really silly mistake but I don't know what's happening. Here's little snippet of my code :
if (tempDeltaDeviation > standardDeltaDeviation) {
Log.e(TAG, "handleMessage: plus $tempDeltaDeviation : $standardDeltaDeviation")
scaleUpAnimation(deltaAnimStep, "Delta", binding.deltaImg)
}
Really basic stuff, right ? Well checking the logs I can see :
handleMessage: plus 1.57756888292539E14 : 7.8364593205657E13
Dunno but last time I was checking 1 was much smaller than 7 so why app enters if statement ?

1.57756888292539E14 is in fact larger than 7.8364593205657E13.
These numbers are represented in scientific notation which is used to work with very small or very large numbers. 1.57756888292539E14 means: 1.57756888292539 * 10^14. By increasing the number after E by one we actually increase the resulting number 10 times. By increasing it by 6, we increase the resulting number million times (10^6 = 1000000).
Making things simple, your numbers are really:
157756888292539 (1.57756888292539 * 100000000000000)
78364593205657 (7.8364593205657 * 10000000000000)
As you can see, the first number is actually bigger.

Related

Why does my looping instruction to input values for a character array of 6 elements stop after 3 inputs?

I'm new to C and programming in general. I'm stuck wondering why this thing is happening. Basically, I wrote this simple program to input a 6 character array from the user, and to print the same out. I'm using CPPDroid on my Android phone to compile and execute the code;
#include"stdio.h"
int main()
{
char c[6];
for(int i=0;i<=5;i++)
{
scanf("%c",&c[i]);
}
for(int j=0;j<=5;j++)
{
printf("%c",c[j]);
}
return 0;
}
For some reason, the first loop simply exits out before the rest of the elements are filled. I'd get an output like this (I entered a,b,s as the first 3 elements):
a
b
s
a
b
s
It just simply only takes 3 elements rather than 6, and prints them back. What's going on?
My apologies if this is a well known issue. I'm not familiar with terms used in programming much, so it's not easy for me to search for questions.
All the answers and comments mentioned it right. I will just add one more thing. Earlier the \n were also taken as input by the scanf. As a result
your loop ended and still the characters you desired were not read.
why the solution scanf("%c ",..) works?
Now, the trailing one is telling scanf() to skip any trailing
whitespace after the character input. It therefore keeps reading input
until it sees something that is not whitespace or the end of the
stream.
Also as pointed out, the leading white space would also let you achieve the same thing with the added benefit of having a smooth interactive input.
To give you an idea of what I mean I would give an excample:
int n,m;
scanf("%d ",&n);
printf("n is %d\n",n);
printf("Give 2nd number\n");
scanf("%d ",&m);
printf("m is %d\n",m);
So now you start giving input.
1
Enter
Now you expect so see the output n is 1. But it seems like it stopped.
You again type 2Enter
Now you see the output: n is 1. Then you see the output
n is 1
6<enter>
Give 2nd number
m is 2
That's what I meant when asked to avoid the trailing whitespace.
When you type in:
a
b
s
The Enter keystroke is counted as its own character (the newline character, '\n'), so you end up storing the following in c: ['a', '\n', 'b', '\n', 's', '\n'].
If you want to consume the newline, you can include it in the scanf() call; something like this:
scanf("\n%c",&c[i]);

Maintain 3 days log - Android

I have a location service in my app. For debug purposes, i want to log (to text file) some events like "new coordinate", "service onDestroy", "service onStartCommand", "coordinate sent to backend", and so on.
But im facing with a problem. The log file gets 350+ new lines each day .. so.. in 3 days i have a file with 1000 lines.
My idea is to maintain only the 3 (or N in that case) last days and delete the content that was written 3+ days ago.
But:
I don't want to check in every write if there are old lines to be removed
I don't want to set an alarm that fires every 3 days to erase the old data.
Can you please tell me if you know another efficient way to handle this situation?
Without a way to index into the text file, there's really no way to solve it without reading each line of the file (up to a certain point), parsing it and finding the date.
Don't keep a file. Keep a database. Have one of the columns be "created time". Then you can easily delete the rows that with a created time older than some threshold.
getContentResolver().delete(
yourUri,
"created_time < ?",
new String[] { System.currentTimeIllis() - Integer.toString(TimeUnit.DAYS.toMillis(3)
);
(Or something, please test your own SQL ...)
As a side note, a 1,000 line text file is nothing. Reading and parsing it is not significant unless you are doing it often. If you read and trimmed the file 1x per day, no problem.
Another solution would be to rotate the files (log --> log.1, log.1 --> log.2, ..., log.n-1 ---> log.n). This of course doesn't set a hard limit on the size of any particular file.

Q: SQLite calculation on android

I am currently working on android application that can track income/outcome of a budget. In one of my database is named Transaction consist of:
Amount
Type (income/outcome)
Date
since this is my first application I'm not really confident with my code, especially in the database. And now I'm making a trigger that can calculate percentage of outcome in a month. Can you check it,if my code is right or not, or not efficient. Here is tiny part of my Trigger code.
In my code I want to calculate percentage of total outcome from the first of a month until the current date .
"CREATE TRIGGER Calc"+
"AFTER INSERT"+
"ON" +transaction+
"FOR EACH ROW" +
"WHEN (SELET * FROM amount.transaction
WHERE (strftime ('%d','now') - strftime('%d','start of month'))
HAVING SUM(amount.transaction WHERE type.transaction IS "outcome") /SUM(amount.transaction WHERE type.transaction IS "income") * 0.01 )"
is it select * from amount.transaction needed? so the code can be much simpler?
0.01 there is 100%
sorry its so messed up since I'm just start in making android and I'm not really well with database. If you have any suggestion please tell me.
Thanks before
I agree with commentors, I don't think you need to use triggers ahead of on-the-fly SELECT statements.
You do need to know that quotations " are literal and when concatenating strings, you need to manually add spaces.

Count until zero

I am beginner and trying to write some calculations with App Inventor 2.
I am trying to write a code to calculate Net present value.
The formula of NPV = - investment + CF/(1+i)power up by years of investment, which means if years of investment are > 1 the second part of formula will repeat until it reached the number of years.
I successfully code the formula for one year that works correct, but have problem with the "repeating" the second part powered by number of years.
I tried to declare years as variable to use it as powering number but think something is wrong with it.
In my opinion I need to split the powering number somewhere to memory and then increase it by 1 until the required number. However have no clue how to do it.
Can anyone help?
Screenshot of the blocks
Following the calculation from the NPB Calculator,
this is converted into blocks the following
Note: for a better clarity and to avoid such long calculation blocks as in your screenshot, I used External Inputs instead of Inline Inputs, which is the default. You can switch that from the context menu after doing a right mouse click onto one of the calculation blocks.
EDIT: screenshot updated for changing cashflows using a list.See also
How to work with Lists by Saj and
How to work with Lists and Lists of lists (pdf) by appinventor.org

First Android app: pictures not refreshing

im very new to Java and Android Programming and am currently trying to write my first Android App.
The App shows 3 Dices, and depending on what combination the dices Show, the App says "yes" "no" or "ultra yes".
My Problem is, when hitting the Button to Re-Roll, sometimes the Pictures of single Dices do not get Updated and then the Answer is wrong (i hope it is somehow clear what i mean)
In this Screenshots you can see what i mean. The first Picture everything is correct (2 dices showing the same numbers means a "No"). Then i hit the Reroll button again and somehow the Pictures didn't get updated.. This occures Randomly, sometimes it works correct 10+ times.
http://image-upload.de/image/gbYhNq/ce3ef664bd.png
http://image-upload.de/image/j8jSTv/97ed7d0f7e.png
The Code of the APP:
http://pastebin.com/360DwFcg
thanks in Advance from a newbie ;)
Your random dice number generation is wrong. It creates a random number from one to seven.
This is probably a long shot, but since your answer is "ultra yes", then all three dice are the same value. So maybe you just got a lucky 7, 7, 7, in which case no images are updated. Just change your random generation to
(int) (Math.random() * 6 + 1)

Categories

Resources