如何添加 NSDecimalNumbers?

发布于 2024-08-25 09:21:24 字数 428 浏览 6 评论 0原文

好吧,这可能是今天最愚蠢的问题,但假设我有一堂课:

NSDecimalNumber *numOne   = [NSDecimalNumber numberWithFloat:1.0];
NSDecimalNumber *numTwo   = [NSDecimalNumber numberWithFloat:2.0];
NSDecimalNumber *numThree = [NSDecimalNumber numberWithFloat:3.0];

为什么我不能有一个函数来添加这些数字:

- (NSDecimalNumber *)addThem {
    return (self.numOne + self.numTwo + self.numThree);
}

我提前为自己是个白痴而道歉,谢谢!

OK this may be the dumb question of the day, but supposing I have a class with :

NSDecimalNumber *numOne   = [NSDecimalNumber numberWithFloat:1.0];
NSDecimalNumber *numTwo   = [NSDecimalNumber numberWithFloat:2.0];
NSDecimalNumber *numThree = [NSDecimalNumber numberWithFloat:3.0];

Why can't I have a function that adds those numbers:

- (NSDecimalNumber *)addThem {
    return (self.numOne + self.numTwo + self.numThree);
}

I apologize in advance for being an idiot, and thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

国产ˉ祖宗 2024-09-01 09:21:24

你不能做你想做的事,因为 C 和 Objective C 都没有运算符重载。相反,你必须这样写:

- (NSDecimalNumber *)addThem {
    return [self.numOne decimalNumberByAdding:
        [self.numTwo decimalNumberByAdding:self.numThree]];
}

如果你愿意玩弄 Objective-C++(将源代码重命名为 .mm),那么你可以这样写:

NSDecimalNumber *operator + (NSDecimalNumber *a, NSDecimalNumber *b) {
    return [a decimalNumberByAdding:b];
}

现在你可以这样写:

- (NSDecimalNumber *)addThem {
    return self.numOne + self.numTwo + self.numThree;
}

Go C++!

You can't do what you want becuase neither C nor Objective C have operator overloading. Instead you have to write:

- (NSDecimalNumber *)addThem {
    return [self.numOne decimalNumberByAdding:
        [self.numTwo decimalNumberByAdding:self.numThree]];
}

If you're willing to play dirty with Objective-C++ (rename your source to .mm), then you could write:

NSDecimalNumber *operator + (NSDecimalNumber *a, NSDecimalNumber *b) {
    return [a decimalNumberByAdding:b];
}

Now you can write:

- (NSDecimalNumber *)addThem {
    return self.numOne + self.numTwo + self.numThree;
}

Go C++!

人间不值得 2024-09-01 09:21:24

NSDecimalNumber 是一个 Objective C 类,实例化时会生成一个包含数字的对象。您只能通过方法访问对象(以及一般对象)。 Objective C 没有办法直接表达对象的算术,因此您需要进行以下三个调用之一:

  • -[NSDecimalNumber doubleValue],它在添加数字之前从对象中提取数字。 link
  • -[NSDecimalNumber DecimalNumberByAdding],它创建一个附加对象,该对象可能比您想要的更多。 link
  • NSDecimalAdd,它再次创建一个附加对象,该对象可能比您想要的更多。 链接

NSDecimalNumber is an Objective C class which, when instantiated, produces an object which contains a number. You access the object (and objects in general) through methods only. Objective C doesn't have a way to directly express arithmetic against objects, so you need to make one of three calls:

  • -[NSDecimalNumber doubleValue], which extracts the numbers from your objects before adding the numbers. link
  • -[NSDecimalNumber decimalNumberByAdding], which creates an additional object, which may be more than you want. link
  • NSDecimalAdd, which, again, creates an additional object, which may be more than you want. link
不羁少年 2024-09-01 09:21:24

请参阅 NSDecimalAdd 函数(以及 NSDecimalMultiply、NSDecimalDivide、NSDecimalSubtract)。

See NSDecimalAdd function (as well as NSDecimalMultiply, NSDecimalDivide, NSDecimalSubtract).

晒暮凉 2024-09-01 09:21:24
[[numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree]
[[numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree]
但可醉心 2024-09-01 09:21:24

您可以:

- (NSDecimalNumber *)addThem {
    return [[self.numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree];
}

您的示例的问题在于 self.numOne 实际上在位级别上是指向对象的指针。所以你的函数将返回一些随机内存位置,而不是总和。

如果 Objective-C 支持 C++ 风格的运算符重载,那么有人可以在应用于两个 NSDecimalNumber 对象时定义 + 作为 decimalNumberByAdding: 的别名。但事实并非如此。

You can:

- (NSDecimalNumber *)addThem {
    return [[self.numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree];
}

The problem with your example is that what self.numOne really is at a bit level is a pointer to an object. So your function would return some random memory location, not the sum.

If Objective-C supported C++-style operator overloading, someone could define + when applied to two NSDecimalNumber objects as an alias to decimalNumberByAdding:. But it doesn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文