0

..so only 3 digit numbers afer "."..

float a = 0.9876543

I would like to have 0.987 only in memory because of transmitting the number over bluetooth..

I'm using iphone SDK..

thank you... :)

Paul R
  • 208,748
  • 37
  • 389
  • 560
vogue
  • 81
  • 1
  • 1
  • 3
  • 3
    A float is a fixed length structure, regardless of the number of digits after the decimal point. Due to the mapping between decimal and binary number systems, rounding to 3dec places may well require more binary "places" than the original number. What's wrong with sending 4 bytes/32 bits (assuming this is the size of float on IOS)? Seems fairly efficient to me. What's "lame"? – spender Mar 09 '11 at 14:31
  • lame is something like a very beginner question.. (but i'm not sure, sorry for my question) so you said, it is the same to send 0.987 and to send 0.987654321 using float number by bluetooth? i will not save the capacity of network? because i need to send 6x2 = 12 float numbers about 30times/second by one way, so it will handle bluetooth with no problem? than i leave full number for sending, thank you – vogue Mar 09 '11 at 14:52

3 Answers3

6

A single precision float takes up the same amount of storage whether you're storing an "easy" number like 1.0 or a "complicated" number like 1.23456789. (Likewise for double precision floats -- they're all the same size as each other, although obviously they take more storage space than single precision floats.)

Any network protocol/transport such a Bluetooth involves overheads in just making the thing work, such as headers etc. These overheads mean that the amount of storage you're wanting to save probably just isn't worth the bother - you're talking about shaving a few bytes off a communication which probably a good few multiples bigger than your potential saving anyway.

A more realistic optimization might be to collect some readings and then transmit them all at once, e.g. 32 at once. That makes your "real information" to "protocol overhead" ratio higher.

Here's some advice about optimization you should be aware of:

http://c2.com/cgi/wiki?PrematureOptimization

Don't optimize too early!

occulus
  • 16,959
  • 6
  • 53
  • 76
1

You have three answers :

the apple way :

float a = 0.9876543;
NSNumber *number = [NSNumber numberWithFloat: a];
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setMaximumFractionDigits:3];

NSLog(@"%@", [formatter stringFromNumber: number]);
[formatter release], formatter = nil;

the Nerd C way :

float a = 0.9876543;
a = roundf(a*1000)*.001f; // or floatf() if you want truncate and not round number

the apple trick :

float a = 0.9876543;
NSString* tmp = [NSString stringWithFormat:@"%.3f",a];
a = [tmp floatValue];

Good luck

xeonarno
  • 426
  • 6
  • 17
0

Are you trying to print the number? Anyway, I'm not sure if this is what you're looking for:

float a = 0.9876543;
NSNumber *number = [NSNumber numberWithFloat: a];
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setMaximumFractionDigits:3];

NSLog(@"%@", [formatter stringFromNumber: number]);
rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • 3
    `NSLog(@"%0.3f", 0.9876543)` :) – hoha Mar 09 '11 at 15:37
  • rainkiz - thank you too, this is very handy :) i will show number for 3 digits (it is enought) but send it full by bluetooth (it is the same long size always.. 4 bits) so thank you! – vogue Mar 09 '11 at 20:22
  • and Dan - thank you too (i'm new at this forum, a little confused about reading comments.. but now i got it... – vogue Mar 09 '11 at 20:29
  • @vogue - glad it helped. Sorry I meant to clarify about the NSLog part, but it looks like you got it anyway. I was assuming that you were going to assign an NSString to some sort of control value or something. – rainkinz Mar 09 '11 at 21:48