java floatmath_【Android】解决FloatMath类中方法在API 23以后不存在问题

1.问题原因分析

在Android SDK更新至23以上时,我们会发现之前在某些地方因计算需要使用到的FloatMath类中的方法如FloatMath.ceil()与FloatMath.sin()等都报错并提示不可用了,FloatMath源码如下:

package android.util;

/**

* Math routines similar to those found in {@link java.lang.Math}.

* 数学例程类似于{@link java.lang.Math}.

*

Historically these methods were faster than the equivalent double-based

* {@link java.lang.Math} methods. On versions of Android with a JIT they

* became slower and have since been re-implemented to wrap calls to

* {@link java.lang.Math}. {@link java.lang.Math} should be used in

* preference.

*

*

All methods were removed from the public API in version 23.

* 所有方法都从版本23的公共API中删除.

* @deprecated Use {@link java.lang.Math} instead.

* 方法过时,应使用java.lang.Math代替.

*/

@Deprecated

public class FloatMath {

/** Prevents instantiation. */

private FloatMath() {}

/**

* Returns the float conversion of the most positive (i.e. closest to

* positive infinity) integer value which is less than the argument.

*

* @param value to be converted

* @return the floor of value

* @removed

*/

public static float floor(float value) {

return (float) Math.floor(value);

}

/**

* Returns the float conversion of the most negative (i.e. closest to

* negative infinity) integer value which is greater than the argument.

*

* @param value to be converted

* @return the ceiling of value

* @removed

*/

public static float ceil(float value) {

return (float) Math.ceil(value);

}

/**

* Returns the closest float approximation of the sine of the argument.

*

* @param angle to compute the cosine of, in radians

* @return the sine of angle

* @removed

*/

public static float sin(float angle) {

return (float) Math.sin(angle);

}

/**

* Returns the closest float approximation of the cosine of the argument.

*

* @param angle to compute the cosine of, in radians

* @return the cosine of angle

* @removed

*/

public static float cos(float angle) {

return (float) Math.cos(angle);

}

/**

* Returns the closest float approximation of the square root of the

* argument.

*

* @param value to compute sqrt of

* @return the square root of value

* @removed

*/

public static float sqrt(float value) {

return (float) Math.sqrt(value);

}

/**

* Returns the closest float approximation of the raising "e" to the power

* of the argument.

*

* @param value to compute the exponential of

* @return the exponential of value

* @removed

*/

public static float exp(float value) {

return (float) Math.exp(value);

}

/**

* Returns the closest float approximation of the result of raising {@code

* x} to the power of {@code y}.

*

* @param x the base of the operation.

* @param y the exponent of the operation.

* @return {@code x} to the power of {@code y}.

* @removed

*/

public static float pow(float x, float y) {

return (float) Math.pow(x, y);

}

/**

* Returns {@code sqrt(}{@code x}{@code 2}{@code +}

* {@code y}{@code 2}{@code )}.

*

* @param x a float number

* @param y a float number

* @return the hypotenuse

* @removed

*/

public static float hypot(float x, float y) {

return (float) Math.hypot(x, y);

}

}

通过观察类说明可知,FloatMath的所有方法都已经从版本23的公共API中删除,官方提示我们可以使用java.lang.Math类来代替使用。

2.解决方法

使用23以下版本的SDK进行编译,即修改gradle.build文件里的compileSdkVersion为23以下版本即可。

使用Math类中的方法进行代替。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值