我构建了一个类并创建了一个初始化所有变量的方法.
在.h
-(void) initWithX:(float *)x_ andY:(float *)y_ andWidth:(float *)width_;
Run Code Online (Sandbox Code Playgroud)
并在.m
-(void) initWithX:(float *)x_ andY:(float *)y_ andWidth:(float *)width_{
[super init];
x = x_; ***
y = y_; ***
width = width_; ***
}
Run Code Online (Sandbox Code Playgroud)
带*的行给我错误"分配中的不兼容类型",但我不明白:我在.h中给出3个浮点数!
谢谢你们
我试图在两个类之间转换,并避免临时对象.
这是我的声明Square:
class CSquare
{
public:
int dimension;
CSquare(int dimension);
// Conversion to Rectangle
operator CRectangle();
~CSquare(void);
};
Run Code Online (Sandbox Code Playgroud)
这是我的声明Rectangle:
class CRectangle
{
public:
int length;
int width;
CRectangle(int length, int width);
//Copy Constructor
CRectangle(const CRectangle& anotherRectangle);
~CRectangle(void);
};
Run Code Online (Sandbox Code Playgroud)
为什么
CSquare aSquare = CSquare(10);
CRectangle convertedRect = (CRectangle) aSquare;
Run Code Online (Sandbox Code Playgroud)
调用复制构造函数?
我有转换功能:
CSquare::operator CRectangle(){
return CRectangle(CSquare::dimension,CSquare::dimension);
}
Run Code Online (Sandbox Code Playgroud)
但我仍然得到一个临时对象.
我该如何避开临时对象?
Float上的truncate方法显然将float截断为整数.那么这里发生了什么:
>> 14820.truncate
=> 14820
>> (148.2 * 100)
=> 14820.0
>> 14820.0.truncate
=> 14820
>> (148.2 * 100).truncate
=> 14819
Run Code Online (Sandbox Code Playgroud)
为什么最后的计算输出不是14820?
如果
NSString sample = @"1sa34hjh#@";
Float 64 floatsample = [sample floatValue];
怎么了?floatsample包含什么?
main()
{
double d1 = 1234.1;
cout << "d1 = 1234.1 --> " << d1 << endl;
double d2 = 1234.099999;
cout << "d2 = 1234.099999 --> " << d2 << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
d1 = 1234.1 --> 1234.1
d2 = 1234.099999 --> 1234.1
Run Code Online (Sandbox Code Playgroud)
我实际上想打印确切的值d2,即1234.099999但不是相同的.
请建议我如何获得确切的价值.
是否有一种快速方法可以将-0.99和+0.99之间的浮点数转换为总是占用4个字符:即正值变为例如'0.03'而负值变为例如'-.03',而没有前导零?显然我能做到
s = '%4.2f' % n
if s[0] == '-':
s = '-%s' % s[2:]
Run Code Online (Sandbox Code Playgroud)
但也许有些stackoverflowers知道Python的快捷方式?
我有类似的东西:
<div id="div1">
<div style="float:left;width:100px;height:100px;"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
并且div1似乎具有0x0大小而不是100x100.为什么?
这会抛出一个异常,表示源无法转换到目标:
int a = 1;
object b = (object)a;
float c = (float)b; // Exception here
Run Code Online (Sandbox Code Playgroud)
为什么?
我有以下代码,我想为float指定一个十进制值而不会丢失精度.
String s1= "525.880005";
Float f = new Float(s1);
System.out.println(f);
Run Code Online (Sandbox Code Playgroud)
产量: 5.88
预期产出: 525.880005