Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 1b28eaf

Browse files
committed
math: Add f64 powf and powi
1 parent 30d0e1b commit 1b28eaf

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

libraries/math/src/instruction.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ pub enum MathInstruction {
9797
argument: f32,
9898
},
9999

100+
/// Pow two float values
101+
///
102+
/// No accounts required for this instruction
103+
F64Pow {
104+
/// The base
105+
base: f64,
106+
/// The exponent
107+
exponent: f64,
108+
},
109+
100110
/// Don't do anything for comparison
101111
///
102112
/// No accounts required for this instruction
@@ -219,6 +229,17 @@ pub fn f32_normal_cdf(argument: f32) -> Instruction {
219229
}
220230
}
221231

232+
/// Create F64Pow instruction
233+
pub fn f64_pow(base: f64, exponent: f64) -> Instruction {
234+
Instruction {
235+
program_id: id(),
236+
accounts: vec![],
237+
data: MathInstruction::F64Pow { base, exponent }
238+
.try_to_vec()
239+
.unwrap(),
240+
}
241+
}
242+
222243
/// Create Noop instruction
223244
pub fn noop() -> Instruction {
224245
Instruction {

libraries/math/src/processor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ pub fn process_instruction(
145145
msg!("{}", result as u64);
146146
Ok(())
147147
}
148+
MathInstruction::F64Pow { base, exponent } => {
149+
msg!("Calculating f64 Pow");
150+
sol_log_compute_units();
151+
let _result = base.powi(5); // ok
152+
let result = base.powf(exponent); // symbol not found
153+
sol_log_compute_units();
154+
msg!("{}", result as u64);
155+
Ok(())
156+
}
148157
MathInstruction::Noop => {
149158
msg!("Do nothing");
150159
msg!("{}", 0_u64);

libraries/math/tests/instruction_count.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ async fn test_f32_normal_cdf() {
194194
banks_client.process_transaction(transaction).await.unwrap();
195195
}
196196

197+
#[tokio::test]
198+
async fn test_f64_pow() {
199+
let mut pc = ProgramTest::new("spl_math", id(), processor!(process_instruction));
200+
201+
pc.set_bpf_compute_max_units(30_000);
202+
203+
let (mut banks_client, payer, recent_blockhash) = pc.start().await;
204+
205+
let mut transaction = Transaction::new_with_payer(
206+
&[instruction::f64_pow(50_f64, 10.5_f64)],
207+
Some(&payer.pubkey()),
208+
);
209+
transaction.sign(&[&payer], recent_blockhash);
210+
banks_client.process_transaction(transaction).await.unwrap();
211+
}
212+
197213
#[tokio::test]
198214
async fn test_noop() {
199215
let mut pc = ProgramTest::new("spl_math", id(), processor!(process_instruction));

0 commit comments

Comments
 (0)