2012-06-13 133 views
0

我正在爲我的遊戲設置音效音量。我希望音量從0.0增加到0.1。浮點計數,負數零?

但是,當它達到零時,我的日誌告訴我它是-0.00000並繼續倒計時。請告訴我什麼是錯的。感謝

-(void)sfxUp{ 
    NSLog(@"SFX UP %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]); 
    if ([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"] == 1.0f) 
     return; 
    [[NSUserDefaults standardUserDefaults]setFloat:([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]+0.1f) forKey:@"sfx_volume"]; 
    [[NSUserDefaults standardUserDefaults]synchronize]; 
    [[SimpleAudioEngine sharedEngine] setEffectsVolume:[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]]; 
    NSLog(@"SFX UP %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]); 
} 
-(void)sfxDown{ 
    NSLog(@"SFX DOWN %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]); 
    if ([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"] == 0.0f) 
     return; 
    [[NSUserDefaults standardUserDefaults]setFloat:([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]-0.1f) forKey:@"sfx_volume"]; 
    [[NSUserDefaults standardUserDefaults]synchronize]; 
    [[SimpleAudioEngine sharedEngine] setEffectsVolume:[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]]; 
    NSLog(@"SFX DOWN %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]); 
} 

點擊只有 '音量調低' 按鈕,這是我的日誌輸出:

2012-06-13 18:32:25.067 Bomb Defuse[80558:1c403] SFX DOWN 1.000000 
2012-06-13 18:32:25.068 Bomb Defuse[80558:1c403] SFX DOWN 0.900000 
2012-06-13 18:32:25.385 Bomb Defuse[80558:1c403] SFX DOWN 0.900000 
2012-06-13 18:32:25.386 Bomb Defuse[80558:1c403] SFX DOWN 0.800000 
2012-06-13 18:32:25.623 Bomb Defuse[80558:1c403] SFX DOWN 0.800000 
2012-06-13 18:32:25.624 Bomb Defuse[80558:1c403] SFX DOWN 0.700000 
2012-06-13 18:32:25.873 Bomb Defuse[80558:1c403] SFX DOWN 0.700000 
2012-06-13 18:32:25.874 Bomb Defuse[80558:1c403] SFX DOWN 0.600000 
2012-06-13 18:32:26.116 Bomb Defuse[80558:1c403] SFX DOWN 0.600000 
2012-06-13 18:32:26.140 Bomb Defuse[80558:1c403] SFX DOWN 0.500000 
2012-06-13 18:32:26.346 Bomb Defuse[80558:1c403] SFX DOWN 0.500000 
2012-06-13 18:32:26.348 Bomb Defuse[80558:1c403] SFX DOWN 0.400000 
2012-06-13 18:32:26.605 Bomb Defuse[80558:1c403] SFX DOWN 0.400000 
2012-06-13 18:32:26.608 Bomb Defuse[80558:1c403] SFX DOWN 0.300000 
2012-06-13 18:32:26.835 Bomb Defuse[80558:1c403] SFX DOWN 0.300000 
2012-06-13 18:32:26.838 Bomb Defuse[80558:1c403] SFX DOWN 0.200000 
2012-06-13 18:32:27.068 Bomb Defuse[80558:1c403] SFX DOWN 0.200000 
2012-06-13 18:32:27.069 Bomb Defuse[80558:1c403] SFX DOWN 0.100000 
2012-06-13 18:32:27.319 Bomb Defuse[80558:1c403] SFX DOWN 0.100000 
2012-06-13 18:32:27.338 Bomb Defuse[80558:1c403] SFX DOWN -0.000000 
2012-06-13 18:32:27.630 Bomb Defuse[80558:1c403] SFX DOWN -0.000000 
2012-06-13 18:32:27.640 Bomb Defuse[80558:1c403] SFX DOWN -0.100000 
2012-06-13 18:32:27.946 Bomb Defuse[80558:1c403] SFX DOWN -0.100000 
2012-06-13 18:32:27.947 Bomb Defuse[80558:1c403] SFX DOWN -0.200000 
2012-06-13 18:32:28.170 Bomb Defuse[80558:1c403] SFX DOWN -0.200000 
2012-06-13 18:32:28.172 Bomb Defuse[80558:1c403] SFX DOWN -0.300000 
2012-06-13 18:32:28.414 Bomb Defuse[80558:1c403] SFX DOWN -0.300000 
2012-06-13 18:32:28.416 Bomb Defuse[80558:1c403] SFX DOWN -0.400000 
2012-06-13 18:32:28.641 Bomb Defuse[80558:1c403] SFX DOWN -0.400000 
2012-06-13 18:32:28.643 Bomb Defuse[80558:1c403] SFX DOWN -0.500000 
2012-06-13 18:32:28.869 Bomb Defuse[80558:1c403] SFX DOWN -0.500000 
2012-06-13 18:32:28.870 Bomb Defuse[80558:1c403] SFX DOWN -0.600000 
2012-06-13 18:32:29.301 Bomb Defuse[80558:1c403] SFX DOWN -0.600000 
2012-06-13 18:32:29.303 Bomb Defuse[80558:1c403] SFX DOWN -0.700000 
2012-06-13 18:32:29.584 Bomb Defuse[80558:1c403] SFX DOWN -0.700000 
2012-06-13 18:32:29.586 Bomb Defuse[80558:1c403] SFX DOWN -0.800000 

編輯:

改爲> =和< =(分別)具有以下輸出:

2012-06-13 18:45:34.574 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:35.135 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:35.529 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:35.877 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:36.057 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:36.229 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:36.391 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:36.542 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:36.717 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:36.897 Bomb Defuse[80607:1c403] BG UP 1.000000 
2012-06-13 18:45:37.599 Bomb Defuse[80607:1c403] BG DOWN1.000000 
2012-06-13 18:45:37.602 Bomb Defuse[80607:1c403] BG DOWN0.900000 
2012-06-13 18:45:38.431 Bomb Defuse[80607:1c403] BG DOWN0.900000 
2012-06-13 18:45:38.491 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:38.617 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:38.951 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:39.125 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:39.479 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:39.908 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:40.399 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:40.624 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:40.841 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
2012-06-13 18:45:41.059 Bomb Defuse[80607:1c403] BG DOWN-0.100000 
+5

[每個計算機科學家應該瞭解的浮點算法](http://docs.sun.com/source/806-3568/ncg_goldberg.html) –

+1

您應該將'== 1.0'更改爲'> = 1.0「和」== 0.0「代表」<= 0.0「,並且你的程序將工作。你也可以使你的步驟'0.125',然後你的確切比較將工作。 – dasblinkenlight

+0

我認爲你錯過的事實是你的結果不完全是'-0'。相反,它可能是一個像'-0.0000000000000001'的值,但是'NSLog'正在截斷值。因此,對於浮點值,你應該總是使用'​​<=' or '> =',幾乎從不'=='。 –

回答

6

Floating poi nt算術不準確 - 從1.0開始並減去0.1十次不一定會給你0.0。有關更多詳細信息,請閱讀經典紙張What Every Computer Scientist Should Know About Floating-Point Arithmetic

,當它改變時,通過避免全等比較(例如,使用>=<=代替==),或者您可以解決此通過蓋住價值[0.0, 1.0] - 理想 - 通過簡單的存儲卷作爲在0和10之間的整數,並在需要時除以10.0。

+0

打我吧!此外,它顯示-0.00000,因爲它被截斷爲0來顯示,但是從負面看。 – dontangg

+0

編輯我原來的帖子 –

+1

另外,請注意,浮點實際上有兩個單獨的表示爲正和負零(32位IEEE754表示00000000和80000000)。他們比較是平等的,但它們並不相同。 – duskwuff