5
5
import android .os .Vibrator ;
6
6
import android .view .View ;
7
7
import android .widget .Button ;
8
+ import android .widget .Toast ;
8
9
import androidx .appcompat .app .AppCompatActivity ;
9
10
import com .example .kalkulator .R ;
10
11
import com .example .kalkulator .listeners .DigitOnClickListener ;
@@ -128,7 +129,11 @@ public void percentOnClick(View v)
128
129
129
130
String formattedOutput = DECIMAL_FORMAT .format (result ).replace ('.' , ',' );
130
131
131
- if (!isOutputTooLong (formattedOutput ))
132
+ if (formattedOutput .equals (INFINITY_SYMBOL ))
133
+ {
134
+ Toast .makeText (this , "Cannot divide by 0" , Toast .LENGTH_SHORT ).show ();
135
+ }
136
+ else if (!isOutputTooLong (formattedOutput ))
132
137
{
133
138
prevValueTextView .setText ("" );
134
139
operationTextView .setText ("" );
@@ -147,9 +152,16 @@ private void logOnClick(View view)
147
152
148
153
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
149
154
150
- currValue = Math .log10 (currValue );
155
+ if (currValue > 0.0D )
156
+ {
157
+ currValue = Math .log10 (currValue );
151
158
152
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
159
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
160
+ }
161
+ else
162
+ {
163
+ Toast .makeText (this , "The number must not be negative" , Toast .LENGTH_SHORT ).show ();
164
+ }
153
165
154
166
makeStandardVibration ();
155
167
}
@@ -160,9 +172,16 @@ private void sqrtOnClick(View view)
160
172
161
173
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
162
174
163
- currValue = Math .sqrt (currValue );
175
+ if (currValue >= 0.0D )
176
+ {
177
+ currValue = Math .sqrt (currValue );
164
178
165
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
179
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
180
+ }
181
+ else
182
+ {
183
+ Toast .makeText (this , "The number must not be negative" , Toast .LENGTH_SHORT ).show ();
184
+ }
166
185
167
186
makeStandardVibration ();
168
187
}
@@ -173,9 +192,16 @@ private void lnOnClick(View view)
173
192
174
193
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
175
194
176
- currValue = Math .log (currValue );
195
+ if (currValue > 0.0D )
196
+ {
197
+ currValue = Math .log (currValue );
177
198
178
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
199
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
200
+ }
201
+ else
202
+ {
203
+ Toast .makeText (this , "The number must not be negative" , Toast .LENGTH_SHORT ).show ();
204
+ }
179
205
180
206
makeStandardVibration ();
181
207
}
@@ -211,10 +237,25 @@ private void tanOnClick(View view)
211
237
String valueText = valueTextView .getText ().toString ();
212
238
213
239
Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
240
+ double delta = 1E-3 ;
214
241
215
- currValue = Math .tan (currValue );
242
+ // find k
243
+ int k = (int ) Math .round ((currValue - Math .PI / 2 ) / Math .PI );
216
244
217
- valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
245
+ // calculate the difference
246
+ double diff = Math .abs (currValue - (Math .PI / 2 + k * Math .PI ));
247
+
248
+ // check if the input is very close to PI/2 + kPI
249
+ if (diff < delta )
250
+ {
251
+ Toast .makeText (this , "The input is very close or equal to PI/2 + kPI, so the result does not exist" , Toast .LENGTH_LONG ).show ();
252
+ }
253
+ else
254
+ {
255
+ currValue = Math .tan (currValue );
256
+
257
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
258
+ }
218
259
219
260
makeStandardVibration ();
220
261
}
@@ -292,7 +333,11 @@ public void equalsOnClick(View v)
292
333
293
334
String formattedOutput = DECIMAL_FORMAT .format (result ).replace ('.' , ',' );
294
335
295
- if (!isOutputTooLong (formattedOutput ))
336
+ if (formattedOutput .equals (INFINITY_SYMBOL ))
337
+ {
338
+ Toast .makeText (this , "Cannot divide by 0" , Toast .LENGTH_SHORT ).show ();
339
+ }
340
+ else if (!isOutputTooLong (formattedOutput ))
296
341
{
297
342
prevValueTextView .setText ("" );
298
343
operationTextView .setText ("" );
0 commit comments