强制类型转换(int)、(int&)和(int*)的区别

GQA数据集介绍 GQA,这是一个用于真实世界视觉推理和组合问答的新数据集。2200万个不同的推理问题,所有这些问题都带有表示其语义的功能程序。答案分布受到严格控制。11.3万张图像、2200万个问题,推理能力有对象和属性识别、传递关系跟踪、空间推理、逻辑推理和比较。 阅读详情

我们先来看两行代码:

float x=1.75,y=1.75;
cout<<(int)x<<" "<<(int&)y<<endl;
输出为:1 1071644672

首先呢,我们来看一下浮点数的格式:
在这里插入图片描述

float是单精度32位,所以呢:

在这里插入图片描述

符号位 (Sign):0代表正数,1代表为负数;
指数位 (Exponent):用于存储科学计数法中的指数数据,指数的数值要加上127,如果指数本来是3,那么要换成130来存储;
尾数部分 (Mantissa):采用移位存储尾数部分,因为M的值一定是1<= M <2,所以它绝对可以写成1.xxxxxxx的形式,所以规定M在存储时舍去第一个1,只存储小数点之后的数字,这样做节省了空间。举例子:
用二进制的科学计数法表示 1000.1,可以表示为1.0001 * 23

                用二进制的科学计数法表示 1110110.1,可以表示为1.1101101 * 26

                任何一个数的科学计数法表示都为1. xxx * 2n ,尾数部分就可以表示为xxxx,由于第一位都是1嘛,干嘛还要表示呀?所以将小数点前面的1省略。

举个例子,浮点数-3.75,先将其转换成2进制的,则为-11.11,可以写成-1.111*2^1,所以符号位是1,加127等于128,指数位是1000 0000,尾数位是111 0000 0000 0000 0000 0000(就是3个1,然后后面20个0,一共23位)。

11000 0000 111 0000 0000 0000 0000 0000

那么1.75呢?1.75写成二进制就是1.11*2^0,所以符号位是0,指数位0+127=127,是0111 1111,尾数位是110 0000 0000 0000 0000 0000(2个1,后面21个0,一共23位),把这几部分写在一起就是:

0011 1111 1110 0000 0000 0000 0000 0000

这个浮点数,在内存里存储的格式就是这样。如果我们把它当做整数来读取,那会是多少呢?

在这里插入图片描述

输出为:1071644672

是不是感觉很巧啊,哈哈,发现点什么了嘛?

(int)x 强制类型转换,是将浮点数x为参数构造整数(即float转换为int)

(int &)y 则是告诉编译器将y看成int对待(内存里的数据不做任何转换),所以(int &)x值为1071 644 672。
至于(int*)的话,我就不多说啦,就是强制转换成整型指针,一般人们容易混淆的是(int)和(int&)这两个。
————————————————
版权声明:本文为CSDN博主「Cookie1997」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yesyes120/article/details/79513719

Landsat9 地表温度反演方法 landsat9地表温度反演及缺失值处理方法 阅读详情

相关推荐

让Claude连续写代码24小时会发生什么?揭秘长时运行”架构背后的工程逻辑与未来

AI长时运行实验揭示软件开发新范式 Claude Opus通过Harness框架24小时独立开发全栈应用的成功实验,展示了AI从辅助工具向异步协作者的进化。实验采用"上下文轮转"机制解决记忆管理难题,将任务拆解为微周期并通过文件系统同步进度。核心突破在于:1)TDD成为AI自治基石,200+测试用例确保开发不偏航;2)文件取代对话成为AI间高效沟通介质;3)开发者角色重构为需求定义和框架设计者。该实验证明,通过严谨的架构约束和验收标准,AI可完成复杂长周期任务,预示着软件开发将从手工作坊

老孙 | Ethan Sun 的博客:AI 智能体与 AI 原生软件的工程实践 1109

int int& int * int**的区别、联系和用途(一)

1、int; int是C++关键字,表示整型,其大小是32位有符号整型,表示的范围是-2,147,483,648 到 2,147,483,647;在声明和定义变量时使用,它表示的意思是所声明或所定义的变量为整型变量。如果其用于函数参数时,其传递方向为值传递,即只能将实参的值传递给形参,而不能将形参的值传递给实参。 例如:通过这种方式去进行交换两个数是无法达到目的的。 例子1:

5万+

【SAP报错】BK128&K5112

在ECC中使用CO1F进行Progressive confirmation时,依次遇到了如下的报错。

weixin_45228526的博客 796

强制转化 (int)、(int&)和(int*)的区别

float a = 1.0f; cout cout cout 输出什么? float b = 0.0f; cout cout cout 输出什么?   输出啥? 不明白 (int &)a是啥意识?为啥两个比较的出结果不同。   —————————————————-

u011432426的专栏 1万+

具身智能与空天具身智能:从理论到实践的跨领域融合

本文深入探讨了具身智能从理论到实践的发展,重点分析了空天具身智能这一前沿领域。文章阐述了如何为AI赋予“身体”,使其通过与环境实时交互进化智能,并详细拆解了构建空天智能体所需的核心技术模块,包括多模态感知、基于大模型的决策规划以及鲁棒控制。同时,结合山区电力线自主巡检等实战案例,展望了多智能体协同与标准化等未来方向。

weixin_29045585的博客 545

《MySQL高级篇》十四、多版本并发控制

《MySQL高级篇》十四、多版本并发控制

LXYDSF的博客 3998

-------int & 引用

这里引用“落辰衰”大佬的解释: 1、int; int是C++关键字,表示整型,其大小是32位有符号整型,表示的范围是-2,147,483,648 到 2,147,483,647;在声明和定义变量时使用,它表示的意思是所声明或所定义的变量为整型变量。 如果其用于函数参数时,其传递方向为值传递,即只能将实参的值传递给形参,而不能将 形参的值传递给实参。 例如:通过这种方式去进行交换两个数...

张洪基 3509

【四足机器人项目实战】四足机器人腿部机械结构设计

系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 TODO:写完再整理 文章目录系列文章目录前言 前言 认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长! 本文先对四足机器人–腿部机械结构设计做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章 提示:以下是本篇文章正文内容 为了减轻腿部惯量,使用有限元分析着重进行轻量化设计,小腿采用工字梁方案,大腿采用内部镂方案,并使用1.5 mm的AL 6061硬质材料,轻量化的腿部设了腿部运动时摆动

qq_35635374的博客 7552

int* 和int&的意思是不是一样

int*是指针,存储地址,可以重新赋值,可以为空。int&是引用,是变量的别名,不能重新绑定,不能为空。它们的用途和特性不同,需要根据具体场景选择使用。

abc81163788的博客 1599

【教程4>第6章>第26节】基于FPGA的MMSE信道估计与均衡系统实现与整体测试,并对比LS信道估计

通过前面几个章节的学习,我们完成了MMSE信道估计和均衡系统。为了进一步验证MMSE信道估计的性能,我们在系统的最后,增加一个信道估计误差计算程序。由于MMSE信道估计,其主要优势在SNR较小时提现,因此测试对比时,我们在testbench中,将SNR设置为2db。然后m值,我们不考虑一个固定值,而是一直累加,直到所设置的仿真时间结束。通过对比可以看到,MMSE信道估计性能优于LS信道估计。这样方便FPGA中的误差计算。

FPGA/MATLAB学习教程/源码/项目合作开发 1605

cesium 深度图(获取区域最高最低点位)

文章摘要: Cesium深度图技术可用于获取区域地形高程数据,核心实现步骤包括:1)通过离屏相机构建独立渲染环境,使用正交投影确保采样精度;2)采用离屏渲染技术获取均匀分布的地形点数据。关键技术点包含多边形外包矩形计算、后台相机配置(5km高度正交投影)、1024×1024像素离屏视图设置,以及深度数据解析(RGBA格式转换和三维坐标还原)。该方法避免了主视图干扰,可精准获取区域内地形高程信息,为填挖方分析等应用提供基础数据支持。

God's asylum的博客 287

(int) &、(int)、(int &)的区别

(int)&, (int), (int &)的区别我们可以首先通过运行以下的程序来分析: int main(){ float a=1.0f; printf("(int)&a=%d\n",(int) &a);      printf("(int&)a=%d\n",(int&)a);     printf("(int)a=%d\n",(int)a); system("pa

Frances_S的漫漫SE路 1344

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

struct address 对齐与用法

#pragma pack (1) // C编译器将按照1个字节对齐。 #pragma pack () // 结束自定义字节对齐方式。 #pragma pack (push,1) // 把原来对齐方式设置压栈,并设新的对齐方式设置为一个字节对齐 #pragma pack(pop) // 结束对齐状态 ...

笔记 846

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

面试题+强制类型转换(int)、(int&)和(int*)知识分析

题目: #include <iostream> #include <stdio.h> #include <string.h> using namespace std; int main() { float a =1.0f; cout << (int)a << endl; cout << &a << endl; cout << (int&amp;)a << endl; co

YLC_慕类的博客 1151

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

(int*) 和 (int&)的区别

(int*) 当需要对一个绝对地址赋值时, 我们需要用(int*)将这个绝对地址转化为内存的地址, 然后再对这个地址进行操作。   int addr = 0x00481c40;     *(int*)addr = 3; 这段代码将绝对地址0x00481c40的内容赋值为3. 这是在32位的系统中, 如果是64位, addr的转化应该为(long*) (int&): 将某个变量

青青的专栏--C/C++ OOA/D Design Pattern 1002

[C/C++](int)a,&a,(int)&a,(int&)a区别小结

1.首先以下述代码为例: #include #include #include #include using namespace std; int main() { float a = 1.0f; cout << (int)a << endl; //将浮点型强制转换为整型1 cout << &a << endl; //取a的地址十六进制00A7FD68 cou

YupingLiu的技术博客 5967

强制类型转换(int)、(int&amp;)和(int*)的区别

我们先来看两行代码:[cpp] view plain copyfloat x=1.75,y=1.75;  cout&lt;&lt;(int)x&lt;&lt;" "&lt;&lt;(int&amp;amp;)y&lt;&lt;endl;  输出为:1    1071644672首先呢,我们来看一下浮点数的格式:float是单精度32位,所以呢:符号位 (Sign):0代表正数,1代表为负数;指数位 (Ex...

jyh的博客 6990

(int&) 与int(&)

#include using namespace std; int main() { float a = 1.0f; cout << sizeof(int) <cout << sizeof(float) < cout << (int)a << endl;//1 cout << &a << endl; /*取a的地址十六进制0012FF7C*/ cout << (int)&a << endl;/*(

nt13的专栏 696

关于对 float 型进行(int)(int*)(int&)

float是单精度32位,按照IEEE754 (32位单精度)标准在计算机中存储。 根据S,E,M。 举个例子: 浮点数-3.75,转换成2进制的,则为-11.11,可以写成-1.111*2^1。E是1+127等于128,1000 0000。M是111 0000 0000 0000 0000 0000(就是3个1,然后后面20个0,一共23位)。 1 1000 0000 111 0000 0000 0000 0000 0000 所以对其: float a=-3.75; int main(){ pri

qq_53318060的博客 662

程序面试题 强制类型转换(int)与 (int&)的区别

一道面试题: 程序代码如下: float a1=1.0f; float b1=0.0f; cout<<boolalpha<<"boolalpha:"<<((int)a1==(int&amp;)a1)<<endl; cout<<boolalpha<<"boolalpha:"<<((int)b...

weixin_30496751的博客 233

(int&)a和(int)a的区别

#include #include #include using namespace std;int main(){ float a = 1.0f; cout << (int)a << endl; cout << (int&)a << endl; cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什

lvbinandylau的专栏 1万+

C++中(int&)和(int)的区别

在说这个问题之前,先说两个需要知道的背景知识: (1)语言的类型的强制转换不会修改原来的数据,会另外的开辟一个临时的或者程序中指定的空间来存储强制转换后的值。 (2)C++引用的实现是在符号表中动了手脚,把自己的变量符号对应的内存地址写成了它所引用的那个变量的内存地址了。 (3)C++的cout函数的执行,是根据变量的名称找到对应的内存地址,然后根据变量的类型从内存中抓取相应的数据。 ...

weixin_34008933的博客 798

(int)a,&a,(int)&a,(int&)a区别

float a = 1.0f; cout << (int)a << endl; //将a强制转换为整型 cout << &a << endl; //取a的地址十六进制 cout << (int)&a << endl; //把a的地址强制转换成十进制的整型 cout << (int&amp;)a << endl; ...

weixin_40791207的博客 536
上一篇: 位域的定义与使用
下一篇: 算法之希尔排序(4)
O0o丶Or n0t
博客等级 码龄8年 4粉丝 43原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值