1.4 提高实数精度

ZOJ 1049

I Think I Need a Houseboat

Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion.


After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0,0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.)


Input Format:

The first line of input will be a positive integer indicating how many data sets will be included (N).

Each of the next N lines will contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers measured in miles. The Y coordinate will be non-negative. (0,0) will not be given.


Output Format:

For each data set, a single line of output should appear. This line should take the form of:

��Property N: This property will begin eroding in year Z.��

Where N is the data set (counting from 1), and Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer.

After the last data set, this should print out ��END OF OUTPUT.��

Notes:

1. No property will appear exactly on the semicircle boundary: it will either be inside or outside.

2. This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines.

3. All locations are given in miles.


Sample Input:

2
1.0 1.0
25.0 0.0


Sample Output:

Property 1: This property will begin eroding in year 1.
Property 2: This property will begin eroding in year 20.

END OF OUTPUT.


package data20180220;

import java.util.Scanner;

public class IThinkINeedAHouse {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        for(int i=1;i<=num;i++){
         double x,y,s;
         x=sc.nextDouble();
         y=sc.nextDouble();
        
         s = 3.14*(Math.pow(x, 2)+Math.pow(y, 2))/2;
        
         int count =(int)Math.ceil(s/50);
         System.out.println("Property "+i+": This property will begin eroding in year "+count+".");    
        }
        System.out.println("END OF OUTPUT.");
    }
}

当日笔记:

向上取整函数:ceil(x)

向下取整函数:floor(x)



<think>好的,用户问的是“单精度浮点型什么意思”。之前用户问过关于C语言程序为什么只能在编程软件里打开的问题,现在转向数据类型,可能是在学习编程基础,特别是C语言的数据类型。需要解释清楚单精度浮点型的基本概念,以及它在计算机中的表示和用途。 首先,用户可能刚接触编程,对浮点数的理解可能有限。需要从基本定义开始,比如单精度和双精度的区别,存储大小,精度范围,以及实际应用场景。同时,用户可能想知道为什么需要单精度,什么时候使用它,以及它的优缺点。 接下来,用户可能对IEEE 754标准不太熟悉,需要简要说明这个标准,包括符号位、指数位和尾数位的分配。这部分内容需要简明扼要,避免过于技术化,但又要准确。比如单精度的32位分配,双精度的64位,以及这些位数如何影响数值的范围和精度。 然后,举例说明单精度的数值范围,比如有效数字大约6-9位,指数范围如何计算,这样用户能有个直观的认识。同时,比较单精度和双精度的不同,帮助用户理解在什么情况下选择哪种类型。 用户可能也会关心实际编程中的应用,比如在C语言中的float类型,内存占用,以及运算速度。这时候需要提到性能与精度的权衡,单精度在图形处理或嵌入式系统中的优势。 还要注意用户可能的误区,比如认为单精度不够精确,或者在所有情况下都应该使用双精度。需要指出单精度的适用场景,以及精度不足时的解决方案,比如使用双精度或高精度库。 最后,总结单精度浮点型的特点,确保用户能够记住关键点。可能需要检查是否有遗漏的重要信息,比如二进制表示的具体例子,或者常见错误,比如精度丢失的问题。确保回答结构清晰,逐步深入,帮助用户逐步理解概念。</think>好的!我将分步骤解释**单精度浮点型**的概念与特性: --- ### 一、基本定义 **单精度浮点型**(Single-Precision Floating-Point)是计算机中表示**实数**的一种数据格式,主要用于存储**小数或极大/极小数值**。其特点为: - **存储空间**:占用4字节(32位二进制) - **命名来源**:相比双精度浮点型(8字节),其精度和存储空间都减半 - **符号规范**:遵循**IEEE 754标准** --- ### 二、二进制结构解析(IEEE 754标准) 将32位划分为三个部分: ``` [符号位 S][指数位 E][尾数位 M] | 1bit | 8bit | 23bit | ``` - **符号位(S)**:0表示正数,1表示负数 - **指数位(E)**:存储偏移127后的指数值(实际指数 = E - 127) - **尾数位(M)**:隐含以1开头的二进制小数部分(实际值 = 1.M) --- ### 三、数值范围与精度 1. **有效数字**:约6-9位十进制精度 2. **数值范围**: - **最大正数**:≈$3.4 \times 10^{38}$ - **最小正数**:≈$1.4 \times 10^{-45}$ 3. **特殊值表示**: - 0(所有位全0) - ±无穷大(指数全1,尾数全0) - NaN非数值(指数全1,尾数非0) --- ### 四、与双精度浮点型对比 | 特征 | 单精度(float) | 双精度(double) | |--------------|-----------------------|-----------------------| | 存储空间 | 4字节(32位) | 8字节(64位) | | 有效数字 | 约6-9位十进制 | 约15-17位十进制 | | 指数范围 | -127~128 | -1023~1024 | | C语言声明 | `float var = 3.14f;` | `double var = 3.14;` | | 典型应用场景 | 图形处理、传感器数据 | 科学计算、金融精度 | --- ### 五、实际编程中的特性 1. **精度丢失示例**: ```c float a = 0.1; // 二进制无法精确表示0.1 float b = 0.2; printf("%d", a + b == 0.3); // 输出0(false) ``` - 解决方法:改用双精度,或允许误差范围(如`fabs(a+b -0.3) < 1e-6`) 2. **内存布局演示**: 数值$-12.375$的单精度表示: - 转换为二进制:$-1100.011 = -1.100011 \times 2^3$ - 符号位:`1` - 指数位:$3 + 127 = 130$ → `10000010` - 尾数位:`10001100000000000000000` - 完整32位:`1 10000010 10001100000000000000000` --- ### 六、应用场景建议 - **优先使用单精度**: - 嵌入式系统(节省内存) - GPU运算(并行计算效率高) - 对精度要求不高的实时计算 - **必须使用双精度**: - 累计误差敏感的场景(如财务计算) - 迭代次数多的科学计算 - 需要高精度坐标定位(如GIS系统) --- ### 七、扩展知识 现代计算机通过**浮点运算单元**(FPU)硬件加速处理浮点数运算。某些场景(如机器学习)还会使用**半精度浮点型**(16位)来进一步提升性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值